Class: Storefront::Components::Form::Builder

Inherits:
Base
  • Object
show all
Defined in:
lib/storefront/components/form/builder.rb

Direct Known Subclasses

Field

Constant Summary

Constants included from Helpers::ContentHelper

Helpers::ContentHelper::SCOPES, Helpers::ContentHelper::SCOPES_WITH_NAMESPACE, Helpers::ContentHelper::SCOPES_WITH_NAMESPACE_AND_NESTED_MODEL, Helpers::ContentHelper::SCOPES_WITH_NESTED_MODEL

Instance Attribute Summary

Attributes inherited from Base

#options, #template

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Helpers::DomHelper

#aria, #clone_attributes, #dom, #index_class, #merge_class, #merge_class!, #page, #resource_to_title, #title_widget, #title_widget_options

Methods included from Helpers::ContentHelper

#encoded_contents, #font_face_data_uri, #html5_time, #read_binary_file, #read_image_size, #rendered_text, #sanitize, #t?

Methods inherited from Base

#component_name, #extract_classes!, #extract_options!, #initialize, #inside?, #pointer, #render_with_pointer, #to_s

Constructor Details

This class inherits a constructor from Storefront::Components::Form::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Storefront::Components::Base

Instance Method Details

#button(*args, &block) ⇒ Object



99
100
101
102
103
104
# File 'lib/storefront/components/form/builder.rb', line 99

def button(*args, &block)
  options = args.extract_options!
  options.reverse_merge!(:as => :submit)
  options[:value] = args.shift || "Submit"
  field(options[:value], options, &block)
end

#field(*args, &block) ⇒ Object



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
# File 'lib/storefront/components/form/builder.rb', line 72

def field(*args, &block)        
  options        = args.extract_options!
  attribute_name = args.shift || attribute.name
  attribute      = Storefront::Attribute.new(
    :name        => attribute_name,
    :model       => @model, 
    :required    => options[:required] == true, 
    :disabled    => options[:disabled] == true,
    :top_level   => options[:attribute] == false
  )

  options.reverse_merge!(
    :template       => template,
    :model          => model,
    :attribute      => attribute, 
    :parent_index   => parent_index,
    :index          => index,
    :field_html     => options[:field_html] || {},
    :input_html     => options[:input_html] || {},
    :label_html     => options[:label_html] || {},
    :error_html     => options[:error_html] || {},
    :hint_html      => options[:hint_html]  || {}
  )

  Storefront::Components::Form::Field.new(options).render(&block)
end

#fields(*args, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/storefront/components/form/builder.rb', line 14

def fields(*args, &block)
  options           = args.extract_options!
  options[:as]      = :fields
  options[:label] ||= false
  attr_name         = args.shift || attribute.name
  template.capture_haml do
    result          = field attr_name, options do |_field|
      template.haml_concat fieldset(&block).gsub(/\n$/, "")
    end
    template.haml_concat result.gsub(/\n$/, "")
  end
end

#fields_for(*args, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
# File 'lib/storefront/components/form/builder.rb', line 27

def fields_for(*args, &block)
  options        = args.extract_options!
  attribute      = args.shift
  macro          = model.macro_for(attribute)
  attr_name      = nil

  if options[:as] == :object
    attr_name = attribute.to_s
  else
    attr_name = config.rename_nested_attributes ? "#{attribute}_attributes" : attribute.to_s
  end

  # do something here for counts
  sub_parent     = model.object
  sub_object     = args.shift

  index          = options.delete(:index)

  unless index.present? && index.is_a?(::String)
    if sub_object.blank? && index.present?
      sub_object   = sub_parent.send(attribute)[index]
    elsif index.blank? && sub_object.present? && macro == :has_many
      index        = sub_parent.send(attribute).index(sub_object)
    end
  end

  sub_object   ||= model.default(attribute) || model.to_s.camelize.constantize.new
  keys           = [model.keys, attr_name]

  model          = Storefront::ModelProxy.new(
    :object      => sub_object, 
    :parent      => sub_parent,
    :keys        => keys
  )

  options.merge!(
    :template       => template,
    :model          => model,
    :parent_index   => index,
    :access_keys    => access_keys, 
    :tabindex       => tabindex
  )
  Storefront::Components::Form::Builder.new(options).render(&block)
end

#fieldset(*args, &block) ⇒ Object



7
8
9
10
11
12
# File 'lib/storefront/components/form/builder.rb', line 7

def fieldset(*args, &block)
  options                     = default_options!(args.extract_options!)
  options[:label]           ||= args.shift

  Storefront::Components::Form::Fieldset.new(options).render(&block)
end

#partial(path, options = {}) ⇒ Object



116
117
118
# File 'lib/storefront/components/form/builder.rb', line 116

def partial(path, options = {})
  @template.render :partial => path, :locals => options.merge(:fields => self)
end

#render {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



120
121
122
# File 'lib/storefront/components/form/builder.rb', line 120

def render(&block)
  yield(self)
end

#submit(*args, &block) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/storefront/components/form/builder.rb', line 106

def submit(*args, &block)
  template.capture_haml do
    result = fieldset :class => config.submit_fieldset_class do |fields|
      template.haml_concat fields.button(*args).gsub(/\n$/, "")
      yield(fields) if block_given?
    end
    template.haml_concat result.gsub(/\n$/, "")
  end
end