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



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

def app
  @app
end

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



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

def block
  @block
end

#indexObject

Returns the value of attribute index

Returns:

  • (Object)

    the current value of index



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

def index
  @index
end

#modelsObject

Returns the value of attribute models

Returns:

  • (Object)

    the current value of models



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

def models
  @models
end

#recordObject

Returns the value of attribute record

Returns:

  • (Object)

    the current value of record



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

def record
  @record
end

Instance Method Details

#association(field_name, options = {}) ⇒ Object



88
89
90
91
# File 'lib/conjoin/form_builder.rb', line 88

def association field_name, options = {}
  options[:is_association] = true
  input field_name, options
end

#errors_for(attr) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/conjoin/form_builder.rb', line 207

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



147
148
149
150
151
152
153
154
155
156
157
158
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
# File 'lib/conjoin/form_builder.rb', line 147

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 do |i, current_record|
        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



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
# File 'lib/conjoin/form_builder.rb', line 98

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

  record_class = record.class.model_name.name.constantize

  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
  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
  })

  new_input = input_class.new data, app, record

  if record_type != 'Hidden' \
  and not options.key(:wrapper) and options[:wrapper] != false
    wrapper field_name.to_s, nested_name, new_input, options
  else
    new_input.render
  end
end

#input_field(field_name, options = {}) ⇒ Object



93
94
95
96
# File 'lib/conjoin/form_builder.rb', line 93

def input_field field_name, options = {}
  options[:wrapper] = false
  input field_name, options
end

#nested_names_for(names) ⇒ Object



81
82
83
84
85
86
# File 'lib/conjoin/form_builder.rb', line 81

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



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/conjoin/form_builder.rb', line 60

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



73
74
75
76
77
78
79
# File 'lib/conjoin/form_builder.rb', line 73

def submit options = {}
  mab do
    input type: 'submit',
      value: options[:value] || 'Submit',
      class: 'btn'
  end
end