Module: Iord::Fields
Instance Method Summary collapse
-
#field_attribute(attr) ⇒ Object
Use for sort_if_enabled which requires the attribute name.
- #field_form(f, attr) ⇒ Object
- #field_form_hash(f, attr) ⇒ Object
- #field_form_object(f, attr) ⇒ Object
- #field_label(f, attr) ⇒ Object
- #field_name(resource, attr = nil) ⇒ Object
- #field_value(resource, attr) ⇒ Object
- #iordh ⇒ Object
Instance Method Details
#field_attribute(attr) ⇒ Object
Use for sort_if_enabled which requires the attribute name
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/iord/fields.rb', line 22 def field_attribute(attr) return 'id' if attr == :_id # default, simply return name return attr unless attr.is_a? Hash # else, Hash return attr[:object] if attr.has_key? :object return attr[:array] if attr.has_key? :array return attr[:value] if attr.has_key? :value return attr[:link] if attr.has_key? :link attr.keys[0] end |
#field_form(f, attr) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/iord/fields.rb', line 58 def field_form(f, attr) if attr.is_a? Symbol return iordh.input(f.label(attr), f.text_field(attr), f.object.errors.(attr)) elsif attr.is_a? Array label = f.label attr[1] field = f.public_send *attr errors = f.object.errors.(attr[1]) return iordh.input(label, field, errors) elsif not attr.is_a? Hash raise ArgumentError, "Unrecognized attr: #{attr}" elsif attr.has_key? :fields return field_form_object(f, attr) else field_form_hash(f, attr) end end |
#field_form_hash(f, attr) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/iord/fields.rb', line 75 def field_form_hash(f, attr) case attr[:field] when Array field = f.public_send *attr[:field] when Hash _attrs = *attr[:field][:helper] field = self.public_send _attrs[0], f, attr, *_attrs[1..-1] else field = f.text_field attr[:field] end return field if attr.has_key? :hidden label = field_label(f, attr) errors = f.object.errors.(attr[:attr]) iordh.input(label, field, errors) end |
#field_form_object(f, attr) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/iord/fields.rb', line 93 def field_form_object(f, attr) multiple_items = attr[:attr].to_s.pluralize == attr[:attr].to_s attr_name = attr[:attr].to_s.singularize.humanize predicate = ->(form, attr) do !(multiple_items and form.object.new_record? and attr.is_a? Hash and attr.has_key? :not_new_record) end html = f.fields_for(attr[:attr]) do |ff| iordh.fieldset(attr_name, attr[:fields], ff, predicate) do if multiple_items iordh.link_to_remove(ff, attr_name) end end end if multiple_items html += iordh.link_to_add(f, attr_name, attr[:attr]) end html.nil? ? String.new : html.html_safe end |
#field_label(f, attr) ⇒ Object
53 54 55 56 |
# File 'lib/iord/fields.rb', line 53 def field_label(f, attr) return f.label *attr[:as] if attr.has_key? :as f.label attr[:attr] end |
#field_name(resource, attr = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/iord/fields.rb', line 35 def field_name(resource, attr = nil) attr = resource if attr.nil? return 'id' if attr == :_id # default, simply return name return attr unless attr.is_a? Hash # else, Hash return attr[:as] if attr.has_key? :as return attr[:object] if attr.has_key? :object return attr[:array] if attr.has_key? :array return attr[:value] if attr.has_key? :value link = attr[:link] link = link.call(resource, attr) if link.respond_to? :call link || attr.keys[0] end |
#field_value(resource, attr) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/iord/fields.rb', line 118 def field_value(resource, attr) # default value for nil return '' if resource.nil? if attr.is_a? Symbol and (attr.to_s.end_with? '_id' or attr == :id or attr == :_id) return resource.public_send(attr).to_s end return resource.public_send(attr) unless attr.is_a? Hash # complex value with Hash if attr.has_key? :array iordh.display_array(resource.public_send(attr[:array]), attr[:attrs]) elsif attr.has_key? :object iordh.display(resource.public_send(attr[:object]), attr[:attrs]) elsif attr.has_key? :value if resource.respond_to? attr[:value] attr[:format].call(resource.public_send(attr[:value])) else attr[:format].call(resource, attr[:value]) end elsif attr.has_key? :image iordh.image resource.public_send(*attr[:image]), attr[:params] elsif attr.has_key? :link label, url = attr[:label], attr[:url] label = label.call(resource, attr) if label.respond_to? :call url = url.call(resource, attr) if url.respond_to? :call iordh.link_to label, url, attr[:params] else field_value(resource.public_send(attr.keys[0]), attr.values[0]) end end |
#iordh ⇒ Object
17 18 19 |
# File 'lib/iord/fields.rb', line 17 def iordh @iordh ||= OutputHelper.new(view_context) end |