Class: SiteFormHelper::SiteFormBuilder

Inherits:
FormtasticBootstrap::FormBuilder
  • Object
show all
Defined in:
app/helpers/site_form_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SiteFormBuilder

Returns a new instance of SiteFormBuilder.



8
9
10
11
# File 'app/helpers/site_form_helper.rb', line 8

def initialize(*args)
   @target = self
   super(*args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



13
14
15
# File 'app/helpers/site_form_helper.rb', line 13

def method_missing(method, *args, &block)
  @target.send(method, *args, &block)
end

Instance Method Details

#action(method, options = {}) ⇒ Object



44
45
46
47
48
# File 'app/helpers/site_form_helper.rb', line 44

def action(method, options = {})
  default_label = I18n.t("fullstack.admin_form.labels.#{method}", :default => "#{method}".humanize)
  options[:type] ||= !!options[:primary] ? :primary : nil
  @target.template.button((options.delete(:label) || default_label), options)
end

#actions(&block) ⇒ Object



17
18
19
# File 'app/helpers/site_form_helper.rb', line 17

def actions(*args, &proc)
  @target.template.form_actions(&proc)      
end

#admin_fields_for(*args) ⇒ Object



179
180
181
182
183
# File 'app/helpers/site_form_helper.rb', line 179

def admin_fields_for(*args)
  @target.semantic_fields_for(*args) do |f|
    yield(FormBuilderDecorator.new(f))
  end
end

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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'app/helpers/site_form_helper.rb', line 207

def association_inputs(association, options = {})
  
  assoc_str = association.to_s
  is_singular = assoc_str.pluralize != assoc_str and assoc_str.singularize == assoc_str
  
  if is_singular
    if @target.template.partial?("nested_belongs_to_#{assoc_str}_fields")
      @target.template.render :partial => "nested_belongs_to_#{assoc_str}_fields", :locals => { 
       :association => association, :f => self, :options => options }
    else
      @target.template.render :partial => "nested_belongs_to_fields", :locals => { 
       :association => association, :f => self, :options => options }
    end

  else
    
    if @target.template.partial?("associated_#{assoc_str}_table")
      @target.template.render :partial => "associated_#{assoc_str}_table", :locals => { 
       :association => association, :f => self, :options => options }
    else
      @target.template.render :partial => "associated_resources_table", :locals => { 
       :association => association, :f => self, :options => options }         
    end

  end
 
end

#box(title, options = {}) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/helpers/site_form_helper.rb', line 185

def box(title, options = {})
  options[:orientation] ||= "form-horizontal"

    @target.template.(:div, :class => ("box " << options[:orientation])) do
      buff = ""
      buff << @target.template.(:div, title, :class => "box-header") 
      buff << @target.template.(:div, :class => "box-content") do
        yield
      end
      buff.html_safe
    end

end

#default_actionObject



21
22
23
# File 'app/helpers/site_form_helper.rb', line 21

def default_action
  action(@target.object.persisted? ? :update : :create, :primary => true)
end

#form_errors(options = {:wrap => true}) ⇒ Object Also known as: errors



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/site_form_helper.rb', line 25

def form_errors(options = {:wrap => true})
  wrap = options.delete(:wrap)
  options[:exclude] ||= [:slug]
  f = @target
  unless f.object.errors.empty?
    if wrap
      @target.template. :div, :class => "alert alert-block" do
        @target.template.link_to "&times;", :class => "close", :data => {:dismiss => "alert"}
        @target.template.(:h4, I18n.t('fullstack.admin.form.correct_these_errors_and_retry', :default => "Correct these errors and retry"), :class => "alert-heading")
        f.semantic_errors *(f.object.errors.keys - (options[:exclude] || []))
      end
    else
      f.semantic_errors *(f.object.errors.keys - (options[:exclude] || []))
    end
    
  end
end

#model_nameObject



4
5
6
# File 'app/helpers/site_form_helper.rb', line 4

def model_name
  super || (object && object.class.model_name)    
end

#resource_inputs(*args) ⇒ Object



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
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
158
159
160
161
162
163
164
165
166
167
# File 'app/helpers/site_form_helper.rb', line 51

def resource_inputs(*args)
  model = @target.object.class
  options = args.extract_options!
  
  only_attributes = options[:only] || []
  except_arg = options[:except] || []
  except_attributes = except_arg + model.protected_attributes.to_a + %W(created_at updated_at slug slugs lat lng position)
  
  only_attributes.map! {|a| a.to_s}
  except_attributes.map! {|a| a.to_s}
  
  columns = model.schema.hierarchy_field_names
  
  # ===============
  # = Attachments =
  # ===============

  attachment_definitions = (model.attachment_definitions || {}).keys
  attachment_columns = attachment_definitions.map {|a|
    ["#{a}_file_name", "#{a}_file_size", "#{a}_content_type", "#{a}_updated_at"]
    }.flatten
        
  columns -= attachment_columns
  columns += attachment_definitions

      
  # ================
  # = Associations =
  # ================

  columns = columns.map {|field_name|
     field_name.to_s.gsub(/_id$/, "").gsub(/_type$/, "")
  }.uniq
  
  belongs_to_associations = model.reflect_on_all_associations(:belongs_to).select {|a|
    !a.options[:polymorphic] && !a.options[:autosave]
  }.map {|assoc| assoc.name.to_s}
  
  polymorphic_associations = model.reflect_on_all_associations(:belongs_to).select {|a|
    a.options[:polymorphic] && !a.options[:autosave]
  }.map {|assoc| assoc.name.to_s}
  
  autosave_belongs_to     = model.reflect_on_all_associations(:belongs_to).select {|a|
    a.options[:autosave]
  }.map {|assoc| assoc.name.to_s}.compact

  has_many_associations = model.reflect_on_all_associations(:has_many).select {|a| 
    a.options[:autosave]
  }.map {|assoc| assoc.name.to_s}
  
  
  
  columns -= polymorphic_associations
  columns += has_many_associations
  
  
  # ====================================
  # = Intersect with :only and :except =
  # ====================================
  
  if only_attributes.any?
    columns = columns.select {|k| only_attributes.include?(k)}
  elsif except_attributes.any?
    columns = columns.delete_if {|k| except_attributes.include?(k)}
  end


  # =============
  # = Rendering =
  # =============
  
  buff = ""
    
  columns.each do |column|
    sym = column.to_sym
    field = model.schema.hierarchy_fields[sym]
    is_belongs_to_associaiton = belongs_to_associations.include?(column)
    is_has_many_association = has_many_associations.include?(column)
    is_autosave_belongs_to = autosave_belongs_to.include?(column)
    
    buff << if is_belongs_to_associaiton
      @target.input(sym, :as => :select)
    
    elsif is_has_many_association || is_autosave_belongs_to
       association_inputs(sym)       

     else
       opts = {}
       args = [sym]
       
       if field && field.options[:markup]
         opts[:as] = :markup

       elsif field && field.options[:in]
         opts[:as] = :select
         opts[:collection] = field.options[:in]
       
       elsif column == "locale"
         opts[:as] = :select
         opts[:collection] = I18n.available_locales.map {|locale| [I18n.t("locale_names.#{locale}", :default => "#{locale}".humanize), locale.to_s] }
         
       else
         nil
       end
       
       args << opts unless opts.empty?
       
       @target.input(*args)

    end
  end
  
  inputs do
    buff.html_safe
  end

end

#resource_submitObject



169
170
171
172
173
# File 'app/helpers/site_form_helper.rb', line 169

def resource_submit
   @target.template.button (@target.object.persisted? ? I18n.t('fullstack.admin.update', :default => "Update") : I18n.t('fullstack.admin.create', :default => "Create")),
    :type => :primary, 
    :size => :large
end

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



200
201
202
203
204
205
# File 'app/helpers/site_form_helper.rb', line 200

def sort_association(association, options = {})
   assoc_str = association.to_s
   @target.template.render :partial => "sort", :locals => { 
    :association => association, :f => self, :options => options }
   
end