storefront 0.2.1 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/Rakefile +2 -2
  2. data/lib/storefront.rb +21 -3
  3. data/lib/storefront/configuration.rb +154 -0
  4. data/lib/storefront/dashboard.rb +8 -7
  5. data/lib/storefront/form.rb +70 -53
  6. data/lib/storefront/form/base.rb +50 -0
  7. data/lib/storefront/form/builder.rb +124 -0
  8. data/lib/storefront/form/errors.rb +20 -46
  9. data/lib/storefront/form/field.rb +102 -0
  10. data/lib/storefront/form/fieldset.rb +44 -0
  11. data/lib/storefront/form/hint.rb +34 -0
  12. data/lib/storefront/form/input.rb +191 -0
  13. data/lib/storefront/form/inputs/checkbox.rb +12 -0
  14. data/lib/storefront/form/inputs/date.rb +16 -0
  15. data/lib/storefront/form/inputs/file.rb +12 -0
  16. data/lib/storefront/form/inputs/hidden.rb +11 -0
  17. data/lib/storefront/form/inputs/radio.rb +11 -0
  18. data/lib/storefront/form/inputs/range.rb +15 -0
  19. data/lib/storefront/form/inputs/select.rb +68 -0
  20. data/lib/storefront/form/inputs/string.rb +89 -0
  21. data/lib/storefront/form/inputs/submit.rb +24 -0
  22. data/lib/storefront/form/inputs/textarea.rb +19 -0
  23. data/lib/storefront/form/inputs/value.rb +16 -0
  24. data/lib/storefront/form/label.rb +35 -0
  25. data/lib/storefront/helpers/attribute_helper.rb +29 -8
  26. data/lib/storefront/helpers/body_helper.rb +1 -1
  27. data/lib/storefront/helpers/cache_helper.rb +2 -2
  28. data/lib/storefront/helpers/component_helper.rb +34 -19
  29. data/lib/storefront/helpers/dashboard_helper.rb +9 -17
  30. data/lib/storefront/helpers/definition_list_helper.rb +82 -14
  31. data/lib/storefront/helpers/form_helper.rb +7 -2
  32. data/lib/storefront/helpers/head_helper.rb +2 -2
  33. data/lib/storefront/helpers/image_helper.rb +1 -25
  34. data/lib/storefront/helpers/list_helper.rb +120 -32
  35. data/lib/storefront/helpers/locale_helper.rb +98 -28
  36. data/lib/storefront/helpers/page_helper.rb +78 -60
  37. data/lib/storefront/helpers/request_helper.rb +3 -0
  38. data/lib/storefront/helpers/table_helper.rb +1 -1
  39. data/lib/storefront/helpers/template_helper.rb +11 -0
  40. data/lib/storefront/helpers/time_helper.rb +2 -2
  41. data/lib/storefront/helpers/widget_helper.rb +153 -2
  42. data/lib/storefront/model_helper.rb +17 -0
  43. data/lib/storefront/proxies/attribute_proxy.rb +150 -0
  44. data/lib/storefront/proxies/model_proxy.rb +249 -0
  45. data/lib/storefront/railtie.rb +4 -2
  46. data/lib/storefront/table.rb +7 -0
  47. data/test/form_helper_test.rb +1 -1
  48. metadata +25 -27
  49. data/init.rb +0 -1
  50. data/lib/storefront/form/elements.rb +0 -101
  51. data/lib/storefront/form/fields.rb +0 -420
  52. data/lib/storefront/form/hints.rb +0 -18
  53. data/lib/storefront/form/inputs.rb +0 -258
  54. data/lib/storefront/form/labels.rb +0 -81
  55. data/lib/storefront/form/model.rb +0 -84
  56. data/lib/storefront/microdata/address.rb +0 -7
  57. data/lib/storefront/microdata/event.rb +0 -13
  58. data/lib/storefront/microdata/geo.rb +0 -7
  59. data/lib/storefront/microdata/organization.rb +0 -7
  60. data/lib/storefront/microdata/person.rb +0 -7
  61. data/lib/storefront/microformat/event.rb +0 -7
  62. data/lib/storefront/microformat/person.rb +0 -7
  63. data/lib/storefront/models/container.rb +0 -0
  64. data/lib/storefront/models/element.rb +0 -15
  65. data/lib/storefront/models/form.rb +0 -0
  66. data/lib/storefront/models/form_element.rb +0 -0
  67. data/lib/storefront/models/link.rb +0 -0
  68. data/lib/storefront/models/list.rb +0 -0
  69. data/lib/storefront/models/list_element.rb +0 -0
  70. data/lib/storefront/models/table.rb +0 -0
  71. data/lib/storefront/models/term.rb +0 -0
  72. data/lib/storefront/models/term_list.rb +0 -0
  73. data/lib/storefront/models/validation.rb +0 -0
data/Rakefile CHANGED
@@ -5,14 +5,14 @@ require 'rake/gempackagetask'
5
5
  spec = Gem::Specification.new do |s|
6
6
  s.name = "storefront"
7
7
  s.authors = ["Lance Pollard"]
8
- s.version = "0.2.1"
8
+ s.version = "0.2.7"
9
9
  s.description = "Semantic HTML5 for Rails"
10
10
  s.summary = "Semantic HTML5 for Rails"
11
11
  s.homepage = "http://github.com/viatropos/storefront"
12
12
  s.email = "lancejpollard@gmail.com"
13
13
  s.rubyforge_project = "storefront"
14
14
  s.platform = Gem::Platform::RUBY
15
- s.files = %w(Rakefile init.rb) + Dir["{lib,rails,test}/**/*"] - Dir["test/tmp"]
15
+ s.files = %w(Rakefile) + Dir["{lib,rails,test}/**/*"] - Dir["test/tmp"]
16
16
  s.require_path = "lib"
17
17
  end
18
18
 
data/lib/storefront.rb CHANGED
@@ -11,11 +11,29 @@
11
11
  # http://github.com/html/once/blob/master/lib/once.rb
12
12
  # http://github.com/fnando/breadcrumbs.git
13
13
  # http://github.com/semanticart/smart-meta
14
- Dir["#{File.expand_path(File.dirname(__FILE__))}/storefront/railtie"]
15
- Dir["#{File.expand_path(File.dirname(__FILE__))}/storefront/helpers/*"].each do |path|
14
+ base = "#{File.expand_path(File.dirname(__FILE__))}/storefront"
15
+ Dir["#{base}/railtie"]
16
+ Dir["#{base}/helpers/*"].each do |path|
16
17
  require path unless ::File.directory?(path)
17
18
  end
19
+
20
+ require "#{base}/form/base"
21
+ require "#{base}/form/input"
22
+ require "#{base}/form/builder"
23
+
18
24
  # make this more explicit
19
- Dir["#{File.expand_path(File.dirname(__FILE__))}/storefront/**/*"].each do |path|
25
+ Dir["#{base}/**/*"].each do |path|
20
26
  require path unless ::File.directory?(path) || path =~ /helpers/
21
27
  end
28
+
29
+ module Storefront
30
+ class << self
31
+ def configure(&block)
32
+ yield configuration
33
+ end
34
+
35
+ def configuration
36
+ @configuration ||= Storefront::Configuration.new
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,154 @@
1
+ module Storefront
2
+ class Configuration
3
+ attr_accessor :field,
4
+ :hint_class,
5
+ :hint_tag,
6
+ :label_class,
7
+ :error_class,
8
+ :error_tag,
9
+ :valid_class,
10
+ :required_class,
11
+ :required_abbr,
12
+ :required_title,
13
+ :optional_class,
14
+ :optional_abbr,
15
+ :optional_title,
16
+ :label_method,
17
+ :label_attribute,
18
+ :validation_max_limit,
19
+ :default_text_field_size,
20
+ :default_text_area_width,
21
+ :all_fields_required_by_default,
22
+ :list_tag,
23
+ :list_class,
24
+ :separator,
25
+ :breadcrumb,
26
+ :include_blank_for_select_by_default,
27
+ :collection_label_methods,
28
+ :i18n_lookups_by_default,
29
+ :rename_nested_attributes,
30
+ :inline_validations,
31
+ :fieldset_class,
32
+ :field_class,
33
+ :legend_class,
34
+ :validate_class,
35
+ :form_class,
36
+ :id_enabled_on,
37
+ :widgets_path,
38
+ :nav_class,
39
+ :include_aria,
40
+ :active_class,
41
+ :nav_tag,
42
+ :terms_tag,
43
+ :term_class,
44
+ :term_key_class,
45
+ :term_value_class,
46
+ :hint_is_popup,
47
+ :field_list_tag,
48
+ :list_partial,
49
+ :definition_list_partial,
50
+ :page_header_id,
51
+ :page_title_id,
52
+ :page_subtitle_id,
53
+ :widget_class,
54
+ :header_class,
55
+ :title_class,
56
+ :subtitle_class,
57
+ :content_class,
58
+ :default_header_level,
59
+ :term_separator,
60
+ :rich_input,
61
+ :submit_fieldset_class,
62
+ :add_label,
63
+ :remove_label,
64
+ :always_include_hint_tag,
65
+ :always_include_error_tag,
66
+ :cycle_fields,
67
+ :require_if_validates_presence,
68
+ # localization
69
+ :localize_with_inheritance,
70
+ :localize_with_namespace,
71
+ :localize_with_nested_model
72
+
73
+ def initialize
74
+ @hint_class = "hint"
75
+ @hint_tag = :figure
76
+ @label_class = "label"
77
+ @required_class = "required"
78
+ @required_abbr = "*"
79
+ @required_title = "Required"
80
+ @error_class = "error"
81
+ @error_tag = :output
82
+ @valid_class = nil
83
+ @optional_class = "optional"
84
+ @optional_abbr = ""
85
+ @optional_title = "Optional"
86
+ @label_method = :humanize
87
+ @label_attribute = :to_label
88
+ @validation_max_limit = 255
89
+ @default_text_field_size = nil
90
+ @default_text_area_width = 300
91
+ @all_fields_required_by_default = true
92
+ @field_list_tag = :ol
93
+ @list_class = "field-list"
94
+ @separator = "-"
95
+ @breadcrumb = " - "
96
+ @include_blank_for_select_by_default = true
97
+ @collection_label_methods = %w(to_label display_name full_name name title to_s)
98
+ @i18n_lookups_by_default = true
99
+ @escape_html_entities_in_hints_and_labels = false
100
+ @rename_nested_attributes = true
101
+ @inline_validations = true
102
+ @fieldset_class = "fieldset"
103
+ @field_class = "field"
104
+ @validate_class = "validate"
105
+ @legend_class = "legend"
106
+ @form_class = "form"
107
+ @id_enabled_on = [] # %w(field label error hint)
108
+ @widgets_path = "shared/widgets"
109
+ @nav_class = "list-item"
110
+ @include_aria = true
111
+ @active_class = "active"
112
+ @nav_tag = :li
113
+ @terms_tag = :dl
114
+ @term_class = "term"
115
+ @term_key_class = "key"
116
+ @term_value_class = "value"
117
+ @hint_is_popup = false
118
+ @list_tag = :ul
119
+ @page_header_id = "header"
120
+ @page_title_id = "title"
121
+ @page_subtitle_id = "subtitle"
122
+ @widget_class = "widget"
123
+ @header_class = "header"
124
+ @title_class = "title"
125
+ @subtitle_class = "subtitle"
126
+ @content_class = "content"
127
+ @default_header_level = 3
128
+ @term_separator = ":"
129
+ @rich_input = true
130
+ @submit_fieldset_class = "submit-fieldset"
131
+ @add_label = "+"
132
+ @remove_label = "-"
133
+ @cycle_fields = false
134
+ @always_include_hint_tag = false
135
+ @always_include_error_tag = true
136
+ @require_if_validates_presence = true
137
+ @localize_with_namespace = false
138
+ @localize_with_nested_model = false
139
+ @localize_with_inheritance = true
140
+ end
141
+
142
+ def get(key)
143
+ send(key).respond_to?(:call) ? send(key).call : send(key)
144
+ end
145
+
146
+ def current_scope
147
+ Thread.current[:storefront_config]
148
+ end
149
+
150
+ def current_scope=(value)
151
+ Thread.current[:storefront_config] = value
152
+ end
153
+ end
154
+ end
@@ -1,6 +1,7 @@
1
- module Storefront
1
+ module Storefront
2
2
  class Dashboard
3
3
  include Storefront::AttributeHelper
4
+ include Storefront::LocaleHelper
4
5
  attr_reader :content, :template, :key, :report, :keys
5
6
 
6
7
  def initialize(template, record_or_key, hash, options = {}, &block)
@@ -18,14 +19,14 @@ module Storefront
18
19
  @report = hash
19
20
  options[:id] ||= "#{record_or_key.to_s}-section"
20
21
  @keys = options.delete(:keys) || {}
21
-
22
+
22
23
  @content = template.capture_haml do
23
24
  template.haml_tag :article, options do
24
25
  yield(self)
25
26
  end
26
27
  end
27
28
  end
28
-
29
+
29
30
  # section :when, :cols => 3
30
31
  def section(*args, &block)
31
32
  attributes = args.extract_options!
@@ -45,7 +46,7 @@ module Storefront
45
46
  template.haml_tag :section, attributes do
46
47
  if @header
47
48
  template.haml_tag :header, :class => "header" do
48
- template.haml_tag :h3, t!(:"#{@section_scope}.title", locale_keys.merge(:scope => :"reports.titles")), :class => "title"
49
+ template.haml_tag :h3, t?(:"#{@section_scope}.title", locale_keys.merge(:scope => :"reports.titles")), :class => "title"
49
50
  if @subtitle.present?
50
51
  template.haml_tag :h4, @subtitle, :class => "subtitle"
51
52
  end
@@ -73,7 +74,7 @@ module Storefront
73
74
  def locale_keys
74
75
  hash = {}
75
76
  keys.each do |key, value|
76
- hash[key] = value.is_a?(::Symbol) ? t!(:"#{value}", :scope => :"reports.keys", :model_names => @model_names) : value
77
+ hash[key] = value.is_a?(::Symbol) ? t?(:"#{value}", :scope => :"reports.keys", :model_names => @model_names) : value
77
78
  end
78
79
  hash.merge(:model_names => @model_names)
79
80
  end
@@ -91,7 +92,7 @@ module Storefront
91
92
  merge_class! attributes, index_class(@column_index, @cols), "#{@column_scope}-column", "col#{@cols}", "column"
92
93
  attributes[:"data-column"] = @column_scope.to_s
93
94
 
94
- title = attributes.delete(:title) || t!(:"#{@section_scope}.#{@column_scope}.title", locale_keys.merge(:scope => :"reports.titles"))
95
+ title = attributes.delete(:title) || t?(:"#{@section_scope}.#{@column_scope}.title", locale_keys.merge(:scope => :"reports.titles"))
95
96
 
96
97
  template.capture_haml do
97
98
  template.capture_haml(&block)
@@ -123,7 +124,7 @@ module Storefront
123
124
 
124
125
  merge_class! attributes, index_class(@row_index, @rows)
125
126
 
126
- template.haml_tag :dt, t!(:"#{@section_scope}.#{@column_scope}.#{@row_scope}", locale_keys.merge(attributes.slice(:past, :present, :future).merge(:scope => :"reports.titles"))), merge_class(attributes, "key")
127
+ template.haml_tag :dt, t?(:"#{@section_scope}.#{@column_scope}.#{@row_scope}", locale_keys.merge(attributes.slice(:past, :present, :future).merge(:scope => :"reports.titles"))), merge_class(attributes, "key")
127
128
  template.haml_tag :dd, report_value, merge_class(attributes.merge(:"data-stat" => @row_scope.to_s), "value")
128
129
  end
129
130
 
@@ -1,65 +1,82 @@
1
1
  module Storefront
2
2
  class Form
3
- attr_reader :content, :template, :object, :parent, :tabindex, :access_keys
4
- include Haml::Helpers
3
+ include Storefront::LocaleHelper
4
+ include Storefront::AttributeHelper
5
5
 
6
- # - form_for @user do |form|
7
- # = form.inputs do
8
- # = form.input :name, :as => :string
9
- # = form.input :email, :as => :email
10
- # = form.partial :name, :values
11
- # novalidate
12
- def initialize(template, *args, &block)
13
- init_haml_helpers
14
- @template = template
15
- options = args.extract_options!
16
- record_or_key = args.shift
17
- if record_or_key.is_a?(::Symbol) || record_or_key.is_a?(::String)
18
- record_or_key = record_or_key.to_s.camelize.constantize.new
19
- end
20
- @object = record_or_key
21
- attributes = options.delete(:html) || {}
22
- attributes[:action] = options.delete(:url)
23
- attributes[:class] = options.delete(:class) if options.has_key?(:class)
24
- attributes[:id] = options.delete(:id) if options.has_key?(:id)
25
- attributes[:multipart] = options.delete(:multipart).to_s if options.has_key?(:multipart)
26
- method = attributes.delete(:method)
27
- if method.blank?
28
- if record_or_key.respond_to?(:new_record?) && !record_or_key.new_record?
29
- method = :put
30
- else
31
- method = :post
32
- end
33
- end
34
-
35
- if method == :get
36
- attributes[:method] = :get
37
- else
38
- attributes[:method] = :post
39
- end
40
- #role="form"
41
- #role="search"
42
- # other roles = button, checkbox, link, menuitem, menuitemcheckbox, menuitemradio, presentation, progressbar, radio, slider, scrollbar, tab, or treeitem
43
- # article, document, application, presentation or main
44
- # http://www.w3.org/TR/wai-aria/roles#widget_roles
6
+ attr_reader :model, :template, :tabindex, :access_keys, :attributes
7
+ delegate :object, :parent, :to => :model
8
+
9
+ def initialize(template, *args)
10
+ options = args.extract_options!
11
+ @template = template
12
+ @model = Storefront::ModelProxy.new(:object => extract_object!(args.shift))
13
+ @attributes = extract_attributes!(options)
14
+ @builder = Storefront::Form::Builder.new(
15
+ :template => template,
16
+ :tabindex => 1,
17
+ :access_keys => {},
18
+ :model => model
19
+ )
20
+ end
45
21
 
46
- @content = template.capture_haml do
22
+ def render(&block)
23
+ template.capture_haml do
47
24
  template.haml_tag :form, attributes do
48
- if block_given?
49
- fields = Storefront::Form::Fields.new(@template,
50
- :tabindex => 1,
51
- :access_keys => {},
52
- :object => @object,
53
- :keys => [@object.class.name.underscore],
54
- &block
55
- )
56
- end
25
+ @builder.render(&block) if block_given?
57
26
 
58
- unless method == attributes[:method]
59
- template.haml_tag :input, :type => :hidden, :name => :_method, :value => method
27
+ unless attributes[:"data-method"] == attributes[:method]
28
+ template.haml_tag :input, :type => :hidden, :name => :_method, :value => attributes[:"data-method"]
60
29
  end
61
30
  end
62
31
  end
63
32
  end
33
+
34
+ protected
35
+ def extract_object!(object)
36
+ if (object.is_a?(::Symbol) || object.is_a?(::String))
37
+ begin
38
+ "::#{object.to_s.camelize}".constantize.new
39
+ rescue Exception => e
40
+ require 'ostruct'
41
+ result = ::OpenStruct.new
42
+ result.klass = object.to_s.camelize
43
+ result
44
+ end
45
+ else
46
+ object
47
+ end
48
+ end
49
+
50
+ def config
51
+ Storefront.configuration
52
+ end
53
+
54
+ def extract_attributes!(options = {})
55
+ attributes = options[:html] || {}
56
+ attributes[:action] = options[:url]
57
+ attributes[:class] = options[:class] if options.has_key?(:class)
58
+ merge_class! attributes, config.form_class
59
+ attributes[:id] = options[:id] if options.has_key?(:id)
60
+ #attributes[:id] ||=
61
+ attributes[:multipart] = "true" if options[:multipart] == "true"
62
+ attributes[:role] = :form
63
+ attributes[:novalidate] = "true" if options[:validate] == false
64
+ attributes[:"data-validate"] = options[:validate].to_s if options.has_key?(:validate)
65
+
66
+ method = attributes[:method] || options[:method]
67
+
68
+ if method.blank?
69
+ if @model.new_record?
70
+ method = :put
71
+ else
72
+ method = :post
73
+ end
74
+ end
75
+
76
+ attributes[:"data-method"] = method
77
+ attributes[:method] = method == :get ? :get : :post
78
+
79
+ attributes
80
+ end
64
81
  end
65
82
  end
@@ -0,0 +1,50 @@
1
+ module Storefront
2
+ class Form
3
+ class Base
4
+ include Storefront::LocaleHelper
5
+ include Storefront::AttributeHelper
6
+
7
+ def initialize(options = {})
8
+ @template = options[:template]
9
+ @model = options[:model]
10
+ @attribute = options[:attribute]
11
+ @parent_index = options[:parent_index]
12
+ @index = options[:index]
13
+ @tabindex = options[:tabindex]
14
+ @access_keys = options[:access_keys]
15
+ @attributes = clone_attributes(options.except(:template, :model, :attribute, :access_keys, :index, :tabindex))
16
+ end
17
+
18
+ protected
19
+
20
+ attr_accessor :template, :model, :attribute, :index, :parent_index
21
+ attr_accessor :attributes, :value, :label, :access_keys, :tabindex, :input_type
22
+
23
+ def default_options!(options = {})
24
+ options[:model] ||= model
25
+ options[:index] ||= index
26
+ options[:attribute] ||= attribute
27
+ options[:template] ||= template
28
+ options
29
+ end
30
+
31
+ def config
32
+ Storefront.configuration
33
+ end
34
+ alias_method :storefront_config, :config
35
+
36
+ def value?
37
+ @value.present?
38
+ end
39
+
40
+ def localize(key, attribute, default, options = {})
41
+ if default.present?
42
+ return default.to_s unless default.is_a?(::Symbol)
43
+ attribute = default
44
+ end
45
+
46
+ t?(attribute, options.reverse_merge(:scope => :"forms.#{key}", :model => model.object))
47
+ end
48
+ end
49
+ end
50
+ end