Class: Conjoin::FormBuilder::Fields
- Inherits:
-
Struct
- Object
- Struct
- Conjoin::FormBuilder::Fields
- Defined in:
- lib/conjoin/form_builder.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#block ⇒ Object
Returns the value of attribute block.
-
#index ⇒ Object
Returns the value of attribute index.
-
#models ⇒ Object
Returns the value of attribute models.
-
#record ⇒ Object
Returns the value of attribute record.
Instance Method Summary collapse
- #association(field_name, options = {}) ⇒ Object
- #errors_for(attr) ⇒ Object
- #fields_for(field_name, options = {}, &block) ⇒ Object
- #input(field_name, options = {}) ⇒ Object
- #input_field(field_name, options = {}) ⇒ Object
- #nested_names_for(names) ⇒ Object
- #render ⇒ Object
- #submit(options = {}) ⇒ Object
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app
58 59 60 |
# File 'lib/conjoin/form_builder.rb', line 58 def app @app end |
#block ⇒ Object
Returns the value of attribute block
58 59 60 |
# File 'lib/conjoin/form_builder.rb', line 58 def block @block end |
#index ⇒ Object
Returns the value of attribute index
58 59 60 |
# File 'lib/conjoin/form_builder.rb', line 58 def index @index end |
#models ⇒ Object
Returns the value of attribute models
58 59 60 |
# File 'lib/conjoin/form_builder.rb', line 58 def models @models end |
#record ⇒ Object
Returns the value of attribute record
58 59 60 |
# File 'lib/conjoin/form_builder.rb', line 58 def record @record end |
Instance Method Details
#association(field_name, options = {}) ⇒ Object
87 88 89 90 |
# File 'lib/conjoin/form_builder.rb', line 87 def association field_name, = {} [:is_association] = true input field_name, end |
#errors_for(attr) ⇒ Object
219 220 221 222 223 224 225 |
# File 'lib/conjoin/form_builder.rb', line 219 def errors_for attr mab do span class: 'has-error has-feedback form-control-feedback' do text! record.errors.[attr.to_sym].try :join, ', ' end end end |
#fields_for(field_name, options = {}, &block) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/conjoin/form_builder.rb', line 159 def fields_for field_name, = {}, &block names = [].concat models associated_record = record.send field_name if scope = .delete(:scope) associated_record = associated_record.send(scope, *.delete(:scope_args)) end if select = .delete(:select) associated_record = Hash[associated_record.each_with_index.map {|a, i| [i, a]}] select.each do |key, select_array| select_array = [select_array] unless select_array.is_a? Array associated_record.select! do |k, v| select_array.include? :"#{v[key]}" end end end if name = .delete(:name) field_name = name end names << "#{field_name}_attributes" if !associated_record.kind_of? ActiveRecord::Associations::CollectionProxy \ and !associated_record.kind_of? ActiveRecord::AssociationRelation \ and !associated_record.kind_of? Array \ and !associated_record.kind_of? Hash fields = Fields.new app, names, associated_record, block, 0 fields.render else html = '' if associated_record.kind_of? Array associated_record.each_with_index do |current_record, i| nested_names = [].concat names nested_names << i fields = Fields.new app, nested_names, current_record, block, i html += fields.render end else associated_record.each_with_index do |current_record, i| nested_names = [].concat names nested_names << i fields = Fields.new app, nested_names, current_record, block, i html += fields.render end end html end # rescue # raise "No associated record #{field_name} for #{record.class}" end |
#input(field_name, options = {}) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 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 154 155 156 157 |
# File 'lib/conjoin/form_builder.rb', line 97 def input field_name, = {} names = [].concat models if .delete(:is_association) names << "#{field_name.to_s.singularize}_ids" names << '' else names << field_name end # create field names that map to the correct models nested_name = nested_names_for names begin record_class = record.class.model_name.name.constantize rescue Exception m = models.join('.').gsub(/_attributes/, '') raise "No value for #{field_name} as #{m} is nil." end if as = .delete(:as) record_type = as.to_s.classify elsif record_class.mini_record_columns \ and mini_column = record_class.mini_record_columns[field_name] \ and input_as = mini_column[:input_as] record_type = input_as.to_s.classify elsif record_class.respond_to?(field_name) && record_class.send(field_name).is_a?(Enumerize::Attribute) record_type = 'Select' else record_type = record_class.columns_hash[field_name.to_s].type.to_s.classify end if mini_column and opts = mini_column[:input_options] = opts.merge end input_class = "Conjoin::FormBuilder::#{record_type}Input".constantize data = OpenStruct.new({ name: nested_name, record: record, value: record.send(field_name), options: , errors: record.errors.[field_name], names: names, field_name: field_name, record_class: record_class }) new_input = input_class.new data, app, record if record_type != 'Hidden' \ and not .key(:wrapper) and [:wrapper] != false if !.key(:label) and [:label] != false wrapper field_name.to_s, nested_name, new_input, else new_input.render end else new_input.render end end |
#input_field(field_name, options = {}) ⇒ Object
92 93 94 95 |
# File 'lib/conjoin/form_builder.rb', line 92 def input_field field_name, = {} [:label] = false input field_name, end |
#nested_names_for(names) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/conjoin/form_builder.rb', line 80 def nested_names_for names # create field names that map to the correct models names.each_with_index.map do |field, i| i != 0 ? "[#{field}]" : field end.join end |
#render ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/conjoin/form_builder.rb', line 59 def render html = block.call(self, index) names = [].concat models names << 'id' mab do if record.id input type: 'hidden', name: nested_names_for(names), value: record.id end text! html end end |
#submit(options = {}) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/conjoin/form_builder.rb', line 72 def submit = {} mab do input type: 'submit', value: [:value] || 'Submit', class: 'btn' end end |