| Module | Access::Sensitive::FormBuilderMixin |
| In: |
lib/access_form_helpers.rb
|
Mixin to add access-sensitive behavior to any FormBuilder class. Causes all input helpers to generate disabled inputs if the current user does not have permission to change the underlying model attribute.
| SIMPLE_FORM_HELPERS | = | %w(file_field text_area password_field hidden_field text_field datetime_select time_select date_select) | explicitly list helpers we wrap; there‘s a test that compares this to what ActiveRecord defines to see if there might be something new in this version of Rails that was missed. | |
| OTHER_FORM_HELPERS | = | %w(check_box radio_button select country_select collection_select time_zone_select) | ||
| JUNK_METHODS | = | %w(fields_for error_message_on error_messages apply_form_for_options! label submit) | ||
| CHECKMARK | = | '✓' |
# File lib/access_form_helpers.rb, line 90
90: def self.included( klass )
91:
92: # The usual hook for ClassMethods
93:
94: klass.extend ClassMethods
95:
96: # Wrap each simple helper in a permission test...
97:
98: SIMPLE_FORM_HELPERS.each do |helper|
99: klass.wrap_form_helper_for_permissions helper,
100: 'attr, html_options = {}'
101: end
102:
103: # Sigh... defaults here not quoted strings (differing from
104: # base Rails behavior) to avoid type_cast crud...
105:
106: klass.wrap_form_helper_for_permissions 'check_box',
107: 'attr, html_options = {}, checked_value=1, unchecked_value=0',
108: '(val == checked_value)? CHECKMARK : ""'
109:
110: klass.wrap_form_helper_for_permissions 'radio_button',
111: 'attr, tag_value, html_options = {}',
112: '(val == tag_value)? CHECKMARK : ""'
113:
114: klass.wrap_form_helper_for_permissions 'select',
115: 'attr, choices, options = {}, html_options = {}',
116: 'find_selected_option_text( val, choices )'
117:
118: # Also assuming that what comes out of .to_s on a TimeZone
119: # object is human-readable... dates are more of a problem,
120: # but this isn't great.
121:
122: klass.wrap_form_helper_for_permissions 'time_zone_select',
123: 'attr, priority_zones = nil, options = {}, html_options = {}'
124:
125: klass.wrap_form_helper_for_permissions 'country_select',
126: 'attr, priority_countries = nil, options = {}, html_options = {}'
127:
128: klass.wrap_form_helper_for_permissions 'collection_select',
129: 'attr, collection, value_attr, text_attr, opts={}, html_options={}',
130: 'find_selected_option_from_collection(val, collection, '+
131: 'value_attr, text_attr)'
132: end
# File lib/access_form_helpers.rb, line 134
134: def find_selected_option_from_collection( val, collection,
135: value_meth, text_meth )
136:
137: return '' if val.nil?
138:
139: collection.each do |elt|
140: if elt.send( value_meth ) == val
141: return elt.send( text_meth )
142: end
143: end
144:
145: return val.to_s # Couldn't find it... punt.
146:
147: end
# File lib/access_form_helpers.rb, line 149
149: def find_selected_option_text( val, collection )
150:
151: if collection.is_a?( Hash )
152: collection = collection.to_a
153: end
154:
155: collection.each do |elt|
156: if elt.respond_to?( :last ) && !elt.is_a?( String )
157: if elt.last == val
158: return elt.first.to_s
159: end
160: else
161: if elt == val
162: return val.to_s
163: end
164: end
165: end
166:
167: # Not found. Could legitimately be nil... other valid
168: # cases are hard to imagine...
169:
170: return val.to_s
171:
172: end