Module: NdrUi::Bootstrap::Readonly
- Included in:
- NdrUi::BootstrapBuilder
- Defined in:
- app/builders/ndr_ui/bootstrap/readonly.rb
Overview
Allows a form to be marked as read-only. Supported field helpers render as a non-editable display instead, allowing a form to be reused as a “show” template.
Most helpers have a similar signature, so can be iterated over and enhanced. The reminaining minority have to be manually re-defined.
Instance Attribute Summary collapse
-
#readonly ⇒ Object
(also: #readonly?)
Returns the value of attribute readonly.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#readonly ⇒ Object Also known as: readonly?
Returns the value of attribute readonly.
113 114 115 |
# File 'app/builders/ndr_ui/bootstrap/readonly.rb', line 113 def readonly @readonly end |
Class Method Details
.included(base) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/builders/ndr_ui/bootstrap/readonly.rb', line 10 def self.included(base) # These have different signatures, or aren't affected by `readonly`: not_affected = %i[label fields_for] needs_custom = %i[radio_button file_field hidden_field textarea] + base. (base.field_helpers - needs_custom - not_affected).each do |selector| class_eval <<-ENDEVAL, __FILE__, __LINE__ + 1 # def text_field(method, options = {}, *rest) # return super unless readonly? # readonly_value = options.symbolize_keys.fetch(:readonly_value, object.send(method)) # @template.content_tag(:p, readonly_value, class: 'form-control-static') # end def #{selector}(method, options = {}, *rest) return super unless readonly? readonly_value = options.symbolize_keys.fetch(:readonly_value, object.send(method)) @template.content_tag(:p, readonly_value, class: 'form-control-static') end ENDEVAL end %i[text_area].each do |selector| class_eval <<-ENDEVAL, __FILE__, __LINE__ + 1 # def text_area(method, options = {}, *rest) # return super unless readonly? # readonly_value = options.symbolize_keys.fetch(:readonly_value, object.send(method)) # @template.simple_format(readonly_value, class: 'form-control-static') # end def #{selector}(method, options = {}, *rest) return super unless readonly? readonly_value = options.symbolize_keys.fetch(:readonly_value, object.send(method)) @template.simple_format(readonly_value, class: 'form-control-static') end ENDEVAL end %i[select time_zone_select].each do |selector| class_eval <<-ENDEVAL, __FILE__, __LINE__ + 1 # def select(method, _something, options = {}, *rest) # return super unless readonly? # readonly_value = options.symbolize_keys.fetch(:readonly_value, object.send(method)) # @template.content_tag(:p, readonly_value, class: 'form-control-static') # end def #{selector}(method, _something, options = {}, *rest) return super unless readonly? readonly_value = options.symbolize_keys.fetch(:readonly_value, object.send(method)) @template.content_tag(:p, readonly_value, class: 'form-control-static') end ENDEVAL end %i[collection_select collection_check_boxes collection_radio_buttons].each do |selector| class_eval <<-ENDEVAL, __FILE__, __LINE__ + 1 # def collection_select(method, collection, value_method, text_method, options = {}, *rest) # return super unless readonly? # readonly_value = options.symbolize_keys.fetch(:readonly_value, object.send(method)) # @template.content_tag(:p, readonly_value, class: 'form-control-static') # end def #{selector}(method, collection, value_method, text_method, options = {}, *rest) return super unless readonly? readonly_value = options.symbolize_keys.fetch(:readonly_value, object.send(method)) @template.content_tag(:p, readonly_value, class: 'form-control-static') end ENDEVAL end class_eval <<-ENDEVAL, __FILE__, __LINE__ + 1 # grouped_collection_select takes many other arguments def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) return super unless readonly? readonly_value = options.symbolize_keys.fetch(:readonly_value, object.send(method)) @template.content_tag(:p, readonly_value, class: 'form-control-static') end # radio_button takes another intermediate argument: def radio_button(method, tag_value, options = {}) return super unless readonly? readonly_value = options.symbolize_keys.fetch(:readonly_value, object.send(method)) @template.content_tag(:p, readonly_value, class: 'form-control-static') end # For file_field, the readonly value defaults to nil: def file_field(method, options = {}) return super unless readonly? readonly_value = options.symbolize_keys[:readonly_value] @template.content_tag(:p, readonly_value, class: 'form-control-static') end # Hidden fields should be suppressed when the form is readonly: def hidden_field(*) super unless readonly? end ENDEVAL class_eval <<-ENDEVAL, __FILE__, __LINE__ + 1 # Allow fields_for to inherit `readonly`: def fields_for(record_name, record_object = nil, fields_options = {}, &block) fields_options[:readonly] ||= readonly super end ENDEVAL end |
Instance Method Details
#initialize ⇒ Object
116 117 118 119 120 |
# File 'app/builders/ndr_ui/bootstrap/readonly.rb', line 116 def initialize(*) super self.readonly = [:readonly] end |