Class: AdminFormHelper::FormBuilderDecorator

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/admin_form_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(decorated) ⇒ FormBuilderDecorator

Returns a new instance of FormBuilderDecorator.



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

def initialize(decorated)
   @target = decorated
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

Instance Method Details

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



39
40
41
42
43
# File 'app/helpers/admin_form_helper.rb', line 39

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



12
13
14
# File 'app/helpers/admin_form_helper.rb', line 12

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

#admin_fields_for(*args) ⇒ Object



174
175
176
177
178
# File 'app/helpers/admin_form_helper.rb', line 174

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

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



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'app/helpers/admin_form_helper.rb', line 202

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



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/helpers/admin_form_helper.rb', line 180

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



16
17
18
# File 'app/helpers/admin_form_helper.rb', line 16

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

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/admin_form_helper.rb', line 20

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

#resource_inputs(*args) ⇒ Object



46
47
48
49
50
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
# File 'app/helpers/admin_form_helper.rb', line 46

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



164
165
166
167
168
# File 'app/helpers/admin_form_helper.rb', line 164

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



195
196
197
198
199
200
# File 'app/helpers/admin_form_helper.rb', line 195

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