Class: Conjoin::FormBuilder::Fields

Inherits:
Struct
  • Object
show all
Defined in:
lib/conjoin/form_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject

Returns the value of attribute app

Returns:

  • (Object)

    the current value of app



58
59
60
# File 'lib/conjoin/form_builder.rb', line 58

def app
  @app
end

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



58
59
60
# File 'lib/conjoin/form_builder.rb', line 58

def block
  @block
end

#indexObject

Returns the value of attribute index

Returns:

  • (Object)

    the current value of index



58
59
60
# File 'lib/conjoin/form_builder.rb', line 58

def index
  @index
end

#modelsObject

Returns the value of attribute models

Returns:

  • (Object)

    the current value of models



58
59
60
# File 'lib/conjoin/form_builder.rb', line 58

def models
  @models
end

#recordObject

Returns the value of attribute record

Returns:

  • (Object)

    the current value of 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, options = {}
  options[:is_association] = true
  input field_name, options
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.messages[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, options = {}, &block
  names = [].concat models

  associated_record = record.send field_name

  if scope = options.delete(:scope)
    associated_record = associated_record.send(scope, *options.delete(:scope_args))
  end

  if select = options.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 = options.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, options = {}
  names = [].concat models
  if options.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 = options.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]
    options = opts.merge options
  end

  input_class = "Conjoin::FormBuilder::#{record_type}Input".constantize

  data = OpenStruct.new({
    name: nested_name,
    record: record,
    value: record.send(field_name),
    options: options,
    errors: record.errors.messages[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 options.key(:wrapper) and options[:wrapper] != false
    if !options.key(:label) and options[:label] != false
      wrapper field_name.to_s, nested_name, new_input, options
    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, options = {}
  options[:label] = false
  input field_name, options
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

#renderObject



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 options = {}
  mab do
    input type: 'submit',
      value: options[:value] || 'Submit',
      class: 'btn'
  end
end