storefront 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,40 +1,42 @@
1
1
  module Storefront
2
- class Form
3
- class Fieldset < Storefront::Form::Base
4
- attr_reader :builder
2
+ module Components
3
+ class Form
4
+ class Fieldset < Storefront::Components::Form::Base
5
+ attr_reader :builder
5
6
 
6
- def initialize(options = {})
7
- super
7
+ def initialize(options = {})
8
+ super
8
9
 
9
- @label = localize(:titles, options[:label], nil, (attributes[:locale_options] || {}).merge(:allow_blank => true)) if options[:label].present?
10
+ @label = localize(:titles, options[:label], nil, (attributes[:locale_options] || {}).merge(:allow_blank => true)) if options[:label].present?
10
11
 
11
- merge_class! attributes, *[
12
- config.fieldset_class
13
- ]
12
+ merge_class! attributes, *[
13
+ config.fieldset_class
14
+ ]
14
15
 
15
- attributes[:id] ||= label.underscore.strip.gsub(/[_\s]+/, config.separator) if label.present?
16
- attributes.delete(:index)
17
- attributes.delete(:parent_index)
18
- attributes.delete(:label)
16
+ attributes[:id] ||= label.underscore.strip.gsub(/[_\s]+/, config.separator) if label.present?
17
+ attributes.delete(:index)
18
+ attributes.delete(:parent_index)
19
+ attributes.delete(:label)
19
20
 
20
- @builder = Storefront::Form::Builder.new(
21
- :template => template,
22
- :model => model,
23
- :attribute => attribute,
24
- :index => index,
25
- :parent_index => parent_index
26
- )
27
- end
21
+ @builder = Storefront::Components::Form::Builder.new(
22
+ :template => template,
23
+ :model => model,
24
+ :attribute => attribute,
25
+ :index => index,
26
+ :parent_index => parent_index
27
+ )
28
+ end
28
29
 
29
- # form.inputs :basic_info, :locale_options => {:count => 1, :past => true}
30
- def render(&block)
31
- template.capture_haml do
32
- template.haml_tag :fieldset, attributes do
33
- template.haml_tag :legend, :class => config.legend_class do
34
- template.haml_tag :span, label
35
- end if label.present?
36
- template.haml_tag config.field_list_tag, :class => config.list_class do
37
- builder.render(&block)
30
+ # form.inputs :basic_info, :locale_options => {:count => 1, :past => true}
31
+ def render(&block)
32
+ template.capture_haml do
33
+ template.haml_tag :fieldset, attributes do
34
+ template.haml_tag :legend, :class => config.legend_class do
35
+ template.haml_tag :span, label
36
+ end if label.present?
37
+ template.haml_tag config.field_list_tag, :class => config.list_class do
38
+ builder.render(&block)
39
+ end
38
40
  end
39
41
  end
40
42
  end
@@ -1,31 +1,33 @@
1
1
  module Storefront
2
- class Form
3
- class Hint < Storefront::Form::Base
4
- def initialize(options = {})
5
- super
2
+ module Components
3
+ class Form
4
+ class Hint < Storefront::Components::Form::Base
5
+ def initialize(options = {})
6
+ super
6
7
 
7
- attributes.merge!(options[:hint_html]) if options[:hint_html]
8
- attributes.delete :hint_html
9
- attributes.delete :hint
8
+ attributes.merge!(options[:hint_html]) if options[:hint_html]
9
+ attributes.delete :hint_html
10
+ attributes.delete :hint
10
11
 
11
- unless options[:hint] == false
12
- merge_class! attributes, config.hint_class
13
- attributes[:id] ||= attribute.to_id(:type => :hint, :index => index, :parent_index => parent_index) if config.id_enabled_on.include?("hint")
14
- if config.include_aria && config.hint_is_popup
15
- attributes[:role] ||= :tooltip
12
+ unless options[:hint] == false
13
+ merge_class! attributes, config.hint_class
14
+ attributes[:id] ||= attribute.to_id(:type => :hint, :index => index, :parent_index => parent_index) if config.id_enabled_on.include?("hint")
15
+ if config.include_aria && config.hint_is_popup
16
+ attributes[:role] ||= :tooltip
17
+ end
18
+ @value = localize(:hints, attribute.name, options[:hint].is_a?(::String) ? options[:hint] : nil, :allow_blank => true)
16
19
  end
17
- @value = localize(:hints, attribute.name, options[:hint].is_a?(::String) ? options[:hint] : nil, :allow_blank => true)
18
20
  end
19
- end
20
21
 
21
- def render(&block)
22
- template.capture_haml do
23
- if value.present?
24
- template.haml_tag config.hint_tag, attributes do
25
- template.haml_concat value.html_safe.gsub(/\n$/, "")
22
+ def render(&block)
23
+ template.capture_haml do
24
+ if value.present?
25
+ template.haml_tag config.hint_tag, attributes do
26
+ template.haml_concat value.html_safe.gsub(/\n$/, "")
27
+ end
28
+ elsif config.always_include_hint_tag
29
+ template.haml_tag config.hint_tag, attributes
26
30
  end
27
- elsif config.always_include_hint_tag
28
- template.haml_tag config.hint_tag, attributes
29
31
  end
30
32
  end
31
33
  end
@@ -1,192 +1,194 @@
1
1
  module Storefront
2
- class Form
3
- class Input < Storefront::Form::Base
4
- class << self
5
- attr_accessor :resolves
6
-
7
- def resolves(*input_types)
8
- @resolves ||= []
9
- @resolves = @resolves.concat(input_types.flatten.compact).uniq
10
- end
2
+ module Components
3
+ class Form
4
+ class Input < Storefront::Components::Form::Base
5
+ class << self
6
+ attr_accessor :resolves
7
+
8
+ def resolves(*input_types)
9
+ @resolves ||= []
10
+ @resolves = @resolves.concat(input_types.flatten.compact).uniq
11
+ end
11
12
 
12
- def resolves?(name)
13
- @resolves ||= []
14
- @resolves.include?(name)
15
- end
13
+ def resolves?(name)
14
+ @resolves ||= []
15
+ @resolves.include?(name)
16
+ end
16
17
 
17
- def registry
18
- @registry ||= Dir[::File.join(::File.dirname(__FILE__), "inputs", "*")].map do |path|
19
- next unless ::File.extname(path) == ".rb"
20
- "Storefront::Form::#{::File.basename(path, ::File.extname(path)).camelize}".constantize
21
- end.compact
22
- end
18
+ def registry
19
+ @registry ||= Dir[::File.join(::File.dirname(__FILE__), "inputs", "*")].map do |path|
20
+ next unless ::File.extname(path) == ".rb"
21
+ "Storefront::Components::Form::#{::File.basename(path, ::File.extname(path)).camelize}".constantize
22
+ end.compact
23
+ end
23
24
 
24
- def find(name)
25
- registry.detect do |input_class|
26
- input_class.resolves?(name)
25
+ def find(name)
26
+ registry.detect do |input_class|
27
+ input_class.resolves?(name)
28
+ end
27
29
  end
28
30
  end
29
- end
30
31
 
31
- def initialize(options = {})
32
- super
32
+ def initialize(options = {})
33
+ super
33
34
 
34
- # input type
35
- options[:as] ||= attribute.input_type(options)
36
- @input_type = options[:as]
37
- @include_blank = options[:include_blank] != false
38
- @value = options[:value]
39
- @dynamic = options[:dynamic] == true
40
- @rich_input = options.has_key?(:rich_input) ? !!options[:rich_input] : config.rich_input
35
+ # input type
36
+ options[:as] ||= attribute.input_type(options)
37
+ @input_type = options[:as]
38
+ @include_blank = options[:include_blank] != false
39
+ @value = options[:value]
40
+ @dynamic = options[:dynamic] == true
41
+ @rich_input = options.has_key?(:rich_input) ? !!options[:rich_input] : config.rich_input
41
42
 
42
- @validate = attributes.delete(:validate) != false
43
+ @validate = attributes.delete(:validate) != false
43
44
 
44
- classes = [input_type, self.class.name.split("::").last.underscore, attribute.name.to_s.underscore.strip.gsub(/[_\s]+/, config.separator)]
45
+ classes = [input_type, self.class.name.split("::").last.underscore, attribute.name.to_s.underscore.strip.gsub(/[_\s]+/, config.separator)]
45
46
 
46
- if options[:input_html].present?
47
- classes << options[:input_html].delete(:class)
48
- end
47
+ if options[:input_html].present?
48
+ classes << options[:input_html].delete(:class)
49
+ end
49
50
 
50
- unless [:submit].include?(input_type)
51
- classes += [
52
- attribute.required? ? config.required_class : config.optional_class,
53
- attribute.errors? ? config.error_class : config.valid_class,
54
- "input"
55
- ]
51
+ unless [:submit].include?(input_type)
52
+ classes += [
53
+ attribute.required? ? config.required_class : config.optional_class,
54
+ attribute.errors? ? config.error_class : config.valid_class,
55
+ "input"
56
+ ]
56
57
 
57
- if @validate && attribute.validations.present?
58
- classes << config.validate_class
58
+ if @validate && attribute.validations.present?
59
+ classes << config.validate_class
60
+ end
59
61
  end
60
- end
61
62
 
62
- # class
63
- merge_class! attributes, *classes.compact.uniq.map(&:to_s)
63
+ # class
64
+ merge_class! attributes, *classes.compact.uniq.map(&:to_s)
64
65
 
65
- # id
66
- attributes[:id] ||= attribute.to_id(:index => index, :parent_index => parent_index) if config.id_enabled_on.include?("input")
66
+ # id
67
+ attributes[:id] ||= attribute.to_id(:index => index, :parent_index => parent_index) if config.id_enabled_on.include?("input")
67
68
 
68
- # validations
69
- if @validate
70
- attributes.merge!(attribute.validations) if config.inline_validations && attribute.validations.present?
71
- end
72
- attributes[:placeholder] = options[:placeholder] if options[:placeholder].present?
69
+ # validations
70
+ if @validate
71
+ attributes.merge!(attribute.validations) if config.inline_validations && attribute.validations.present?
72
+ end
73
+ attributes[:placeholder] = options[:placeholder] if options[:placeholder].present?
73
74
 
74
- # name
75
- attributes[:name] ||= attribute.to_param(:index => index, :parent_index => parent_index)
75
+ # name
76
+ attributes[:name] ||= attribute.to_param(:index => index, :parent_index => parent_index)
76
77
 
77
- # value
78
- attributes[:value] ||= attribute.value(options[:value])
78
+ # value
79
+ attributes[:value] ||= attribute.value(options[:value])
79
80
 
80
- # attributes[:tabindex] = @tabindex
81
- attributes[:maxlength] = options[:max] if options[:max].present?
81
+ # attributes[:tabindex] = @tabindex
82
+ attributes[:maxlength] = options[:max] if options[:max].present?
82
83
 
83
- # expressions
84
- pattern = options[:match]
85
- pattern = pattern.source if pattern.is_a?(::Regexp)
86
- attributes[:"data-match"] = pattern if pattern.present?
87
- attributes[:"aria-required"] = attribute.required.to_s if attribute.required?
84
+ # expressions
85
+ pattern = options[:match]
86
+ pattern = pattern.source if pattern.is_a?(::Regexp)
87
+ attributes[:"data-match"] = pattern if pattern.present?
88
+ attributes[:"aria-required"] = attribute.required.to_s if attribute.required?
88
89
 
89
- # access key
90
- access_key = attributes[:accesskey] || attribute.access_key
91
- attributes[:accesskey] = access_key
90
+ # access key
91
+ access_key = attributes[:accesskey] || attribute.access_key
92
+ attributes[:accesskey] = access_key
92
93
 
93
- attributes.merge!(options[:input_html]) if options[:input_html]
94
+ attributes.merge!(options[:input_html]) if options[:input_html]
94
95
 
95
- attributes.delete :include_blank
96
- attributes.delete :input_html
97
- attributes.delete :include_template
98
- attributes.delete :as
99
- attributes.delete :dynamic
100
- attributes.delete :parent_index
96
+ attributes.delete :include_blank
97
+ attributes.delete :input_html
98
+ attributes.delete :include_template
99
+ attributes.delete :as
100
+ attributes.delete :dynamic
101
+ attributes.delete :parent_index
101
102
 
102
- attributes[:required] = "true" if attributes.delete(:required) == true
103
- attributes[:disabled] = "true" if attributes.delete(:disabled) == true
104
- attributes[:autofocus] = "true" if attributes.delete(:autofocus) == true
105
- attributes[:"data-dynamic"] = "true" if @dynamic
103
+ attributes[:required] = "true" if attributes.delete(:required) == true
104
+ attributes[:disabled] = "true" if attributes.delete(:disabled) == true
105
+ attributes[:autofocus] = "true" if attributes.delete(:autofocus) == true
106
+ attributes[:"data-dynamic"] = "true" if @dynamic
106
107
 
107
- attributes[:title] ||= attributes[:placeholder] if attributes[:placeholder].present?
108
+ attributes[:title] ||= attributes[:placeholder] if attributes[:placeholder].present?
108
109
 
109
- autocomplete = attributes.delete(:autocomplete)
110
- if autocomplete && config.include_aria
111
- attributes[:"aria-autocomplete"] = case autocomplete
112
- when :inline, :list, :both
113
- autocomplete.to_s
114
- else
115
- "both"
110
+ autocomplete = attributes.delete(:autocomplete)
111
+ if autocomplete && config.include_aria
112
+ attributes[:"aria-autocomplete"] = case autocomplete
113
+ when :inline, :list, :both
114
+ autocomplete.to_s
115
+ else
116
+ "both"
117
+ end
116
118
  end
117
119
  end
118
- end
119
120
 
120
- def render(&block)
121
- send(:"#{@input_type}_input")
122
- end
121
+ def render(&block)
122
+ send(:"#{@input_type}_input")
123
+ end
123
124
 
124
- protected
125
- attr_reader :include_blank
125
+ protected
126
+ attr_reader :include_blank
126
127
 
127
- def simple_input(type, options = {}, &block)
128
- template.capture_haml do
129
- template.haml_tag :input, extract_attributes!(type, options), &block
130
- if @dynamic
131
- template.haml_tag :ul, :class => "buttons" do
132
- template.haml_tag :li do
133
- template.haml_tag :a, config.add_label, :class => :add, :role => :button, :type => :button, :href => "#"
134
- end
135
- template.haml_tag :li do
136
- template.haml_tag :a, config.remove_label, :class => :remove, :role => :button, :type => :button, :href => "#"
128
+ def simple_input(type, options = {}, &block)
129
+ template.capture_haml do
130
+ template.haml_tag :input, extract_attributes!(type, options), &block
131
+ if @dynamic
132
+ template.haml_tag :ul, :class => "buttons" do
133
+ template.haml_tag :li do
134
+ template.haml_tag :a, config.add_label, :class => :add, :role => :button, :type => :button, :href => "#"
135
+ end
136
+ template.haml_tag :li do
137
+ template.haml_tag :a, config.remove_label, :class => :remove, :role => :button, :type => :button, :href => "#"
138
+ end
137
139
  end
138
140
  end
139
141
  end
140
142
  end
141
- end
142
143
 
143
- def rich_input(type, options = {}, &block)
144
- template.capture_haml do
145
- result = template.rich_button :inner_html => extract_attributes!(type, options), :as => :input
146
- template.haml_concat result.gsub(/\n$/, "") if result
144
+ def rich_input(type, options = {}, &block)
145
+ template.capture_haml do
146
+ result = template.rich_button :inner_html => extract_attributes!(type, options), :as => :input
147
+ template.haml_concat result.gsub(/\n$/, "") if result
148
+ end
147
149
  end
148
- end
149
150
 
150
- def extract_attributes!(type, options = {})
151
- if [:numeric, :string, :password, :text, :phone, :url, :email].include?(type)
152
- attributes = model.default_string_options(attribute.name, type).merge(self.attributes)
153
- else
154
- attributes = self.attributes
155
- end
151
+ def extract_attributes!(type, options = {})
152
+ if [:numeric, :string, :password, :text, :phone, :url, :email].include?(type)
153
+ attributes = model.default_string_options(attribute.name, type).merge(self.attributes)
154
+ else
155
+ attributes = self.attributes
156
+ end
156
157
 
157
- merge_class! attributes, options.delete(:class)
158
+ merge_class! attributes, options.delete(:class)
158
159
 
159
- attributes[:type] = input_type_for(type)
160
+ attributes[:type] = input_type_for(type)
160
161
 
161
- attributes.merge(options)
162
- end
162
+ attributes.merge(options)
163
+ end
163
164
 
164
- def base_input(tag, *args, &block)
165
- options = args.extract_options!
166
- attributes = self.attributes
167
- text = args.shift
168
-
169
- template.capture_haml do
170
- if block_given?
171
- template.haml_tag tag, attributes, &block
172
- else
173
- if text.present?
174
- template.haml_tag tag, text, attributes
165
+ def base_input(tag, *args, &block)
166
+ options = args.extract_options!
167
+ attributes = self.attributes
168
+ text = args.shift
169
+
170
+ template.capture_haml do
171
+ if block_given?
172
+ template.haml_tag tag, attributes, &block
175
173
  else
176
- template.haml_tag tag, attributes
174
+ if text.present?
175
+ template.haml_tag tag, text, attributes
176
+ else
177
+ template.haml_tag tag, attributes
178
+ end
177
179
  end
178
180
  end
179
181
  end
180
- end
181
182
 
182
- def input_type_for(type)
183
- case type
184
- when :password, :text, :url, :email, :file, :textarea, :search, :submit, :radio, :checkbox
185
- type
186
- when :phone, :tel
187
- :tel
188
- else
189
- :string
183
+ def input_type_for(type)
184
+ case type
185
+ when :password, :text, :url, :email, :file, :textarea, :search, :submit, :radio, :checkbox
186
+ type
187
+ when :phone, :tel
188
+ :tel
189
+ else
190
+ :string
191
+ end
190
192
  end
191
193
  end
192
194
  end