attributes_for 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a209b072da0d34c383a199a10190e3b747aa993
4
- data.tar.gz: 7de6c1c29fd8eb272e51176376f894cea70dbf71
3
+ metadata.gz: 3341f8ea5c6de31880faa96f8566ae49c79b770d
4
+ data.tar.gz: 9e0d2c45a2be18135fcd985cae66f50cc4e7827f
5
5
  SHA512:
6
- metadata.gz: 4cd8f89690b7249894fbffb68f7ae2e10b9b4abf305e0dfc0b0a8f25d49fdc6d9910c8f13b3bb5c82fd08f17fe81222d52a645aebeb9c05861ac494958358408
7
- data.tar.gz: f94caba36425d9dc8226557a486259df7264c116f0514ca697ed2fa5d651c5627cb3f791ef60eb049779a1d9a00494c7ffab1f4d1c4291e8966f8901ca747ea8
6
+ metadata.gz: d84163fccde09263dad397d39c36eb7d4d9c3fb3ec2dce7771af6f8c54a81f8968cf952af767149cf2070ff4447f9fbce0924d65f265396c640b8cf719f89b54
7
+ data.tar.gz: 09b735c044487a75e06ab3d6b3fde5b3d436cff6672adb7d445de37d7cef7b4413db0a83d2372194d89e6eaa826762790f93042917994e415b31a0ac54dbe771
@@ -3,4 +3,5 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.1.0
5
5
  - 2.2.2
6
+ - 2.2.3
6
7
  before_install: gem install bundler -v 1.10.3
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.7.0] - 2015-11-09
4
+ ### Added
5
+ - Wrap label and value in tags.
6
+ - `:defaults` options. Apply same options to all elements.
7
+ - `:wrapper` options (change default wrappers).
8
+ - `:label_html` and `:value_html` to set attributes on tags.
9
+ ### Changed
10
+ - Improved implementation slightly.
11
+
12
+ ## [0.6.1] - 2015-10-29
13
+ ### Changed
14
+ - Updated dependencies.
15
+
16
+ ## [0.6.0] - 2015-10-21
17
+ ### Added
18
+ - `date` and `datetime` helpers formats/converts to date and datetime.
19
+
3
20
  ## [0.5.1] - 2015-12-10
4
21
  ### Fixed
5
22
  - `attribute` helper explicitly casts integer to string.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attributes_for (0.6.1)
4
+ attributes_for (0.7.0)
5
5
  chronic_duration (~> 0.10)
6
6
  font-awesome-rails (~> 4.0)
7
7
  phony (~> 2.0)
@@ -74,11 +74,11 @@ GEM
74
74
  mime-types (2.6.2)
75
75
  mini_portile (0.6.2)
76
76
  minitest (5.8.2)
77
- netrc (0.10.3)
77
+ netrc (0.11.0)
78
78
  nokogiri (1.6.6.2)
79
79
  mini_portile (~> 0.6.0)
80
80
  numerizer (0.1.1)
81
- phony (2.15.6)
81
+ phony (2.15.7)
82
82
  rack (1.6.4)
83
83
  rack-test (0.6.3)
84
84
  rack (>= 1.0)
data/README.md CHANGED
@@ -56,6 +56,8 @@ Present attributes using the API in the example below. It can also generate a
56
56
  standard label value pair given an attribute name using the `attribute` method. Arbitrary
57
57
  strings can also be presented using the `string` method.
58
58
 
59
+ Here is a complete example using all possible options.
60
+
59
61
  ```erb
60
62
  <ul class="list-unstyled">
61
63
  <%= attributes_for @company do |b| %>
@@ -83,6 +85,50 @@ strings can also be presented using the `string` method.
83
85
  </ul>
84
86
  ```
85
87
 
88
+ Passing `:label_html` or `:value_html` to `#attribute` adds the given
89
+ options to the `label` and `value` wrapper tags.
90
+
91
+ ```erb
92
+ <ul class="list-unstyled">
93
+ <%= attributes_for @product, defaults: { label_html: { class: 'label' } } do |b|
94
+ <li><%= b.attribute :price, icon: 'money', value_html: { class: 'pull-right' } %></li>
95
+ <% end %>
96
+ </ul>
97
+ ```
98
+
99
+ Passing `:defaults` to `attributes_for` gives the option to all
100
+ attributes. This option you can typically use when you want add styling
101
+ to the label of each attribute.
102
+
103
+ ```erb
104
+ <ul class="list-unstyled">
105
+ <%= attributes_for @company, defaults: { label_html: { class: 'label' } } do |b| %>
106
+ <li><%= b.attribute :name, icon: 'building-o' %></li>
107
+ <% end %>
108
+ </ul>
109
+ ```
110
+
111
+ `attributes_for` wraps labels and values in `span` tags by default. Passing
112
+ `:wrappers` to `attributes_for` gives you the option to change the wrapper.
113
+
114
+ ```erb
115
+ <ul class="list-unstyled">
116
+ <%= attributes_for @company, wrappers: { label: 'strong' } do |b| %>
117
+ <li><%= b.attribute :name, icon: 'building-o' %></li>
118
+ <% end %>
119
+ </ul>
120
+ ```
121
+
122
+ Passing `:empty` to `attributes_for` overrides the default empty value.
123
+
124
+ ```erb
125
+ <dl>
126
+ <%= attributes_for @company, wrappers: { label: 'dt', value: 'dd' }, empty: '-' do |b| %>
127
+ <%= b.attribute :fax %>
128
+ <% end %>
129
+ </dl>
130
+ ```
131
+
86
132
  ## Options
87
133
 
88
134
  Available options:
@@ -3,17 +3,25 @@ module AttributesFor
3
3
  module AttributesForHelper
4
4
 
5
5
  def attributes_for(object, options = {}, &block)
6
- capture AttributeBuilder.new(object, self), &block
6
+ capture AttributeBuilder.new(object, self, options), &block
7
7
  end
8
8
 
9
9
  class AttributeBuilder
10
10
  include ActionView::Helpers
11
11
  include FontAwesome::Rails::IconHelper
12
12
 
13
- attr_accessor :object, :template
13
+ attr_accessor :object, :template, :default_options, :wrappers,
14
+ :options
14
15
 
15
- def initialize(object, template)
16
- @object, @template = object, template
16
+ def initialize(object, template, options = {})
17
+ @object = object
18
+ @template = template
19
+ @default_options = options[:defaults] || {}
20
+
21
+ @wrappers = { label: 'span', value: 'span' }
22
+ @wrappers.merge!(options.delete(:wrappers)) if options.key?(:wrappers)
23
+
24
+ @empty = options.delete(:empty) if options.key?(:empty)
17
25
  end
18
26
 
19
27
  def method_missing(method, *args, &block)
@@ -26,75 +34,108 @@ module AttributesFor
26
34
 
27
35
  private
28
36
 
29
- def build_content(method, attribute_name, options = {}, &block)
30
- content = format_attribute(method, attribute_name, options, &block)
31
-
32
- options[:icon] ||= icon_map(method)
33
-
34
- wrap_content(human_name(attribute_name), content, options)
37
+ def empty_value
38
+ @empty || I18n.t('attributes_for.not_set')
35
39
  end
36
40
 
37
- def format_attribute(method, attribute_name, options = {}, &block)
38
- value = object.public_send(attribute_name)
39
-
41
+ def build_value(method, attribute_name, options = {}, &block)
40
42
  if block_given?
41
43
  template.capture(&block)
42
- elsif value.to_s.empty?
43
- I18n.t "attributes_for.not_set"
44
44
  else
45
- case method.to_sym
46
- when :boolean
47
- I18n.t("attributes_for.#{value.to_s}")
48
- when :date
49
- I18n.l(value.to_date, format: options[:format])
50
- when :datetime
51
- I18n.l(value, format: options[:format])
52
- when :duration
53
- ChronicDuration.output(value, :keep_zero => true)
54
- when :email
55
- mail_to(value, value, title: human_name(attribute_name))
56
- when :phone
57
- phone_number = Phony.format(Phony.normalize(value.to_s), format: :international)
58
- link_to(phone_number, "tel:#{phone_number}", title: human_name(attribute_name))
59
- when :url
60
- link_to(value, value, title: human_name(attribute_name))
45
+ value = object.public_send(attribute_name)
46
+ if value.to_s.empty?
47
+ empty_value
61
48
  else
62
- value.to_s
49
+ format_value(
50
+ method,
51
+ value,
52
+ options.reverse_merge(title: human_name(attribute_name))
53
+ )
63
54
  end
64
55
  end
65
56
  end
66
57
 
67
- def wrap_content(label, content, options)
68
- html_options = options[:html] || {}
69
- html_options[:id] = options.delete(:id) if options.key?(:id)
70
- html_options[:class] = options.delete(:class) if options.key?(:class)
58
+ def build_content(method, attribute_name, options = {}, &block)
59
+ value = build_value(method, attribute_name, options, &block)
60
+
61
+ options[:icon] ||= icon_map(method)
62
+
63
+ wrap_content(human_name(attribute_name), value, options)
64
+ end
71
65
 
72
- content = "#{label}: " + content unless options[:label] === false
73
- content = content_tag(:span, content.html_safe, html_options)
66
+ def format_value(method, value, options = {})
67
+ case method.to_sym
68
+ when :boolean
69
+ I18n.t("attributes_for.#{value}")
70
+ when :date
71
+ I18n.l(value.to_date, format: options[:format])
72
+ when :datetime
73
+ I18n.l(value, format: options[:format])
74
+ when :duration
75
+ ChronicDuration.output(value, keep_zero: true)
76
+ when :email
77
+ mail_to(value, value, title: options[:title])
78
+ when :phone
79
+ phone_number = Phony.format(Phony.normalize(value.to_s),
80
+ format: :international)
81
+ link_to(phone_number, "tel:#{phone_number}", title: options[:title])
82
+ when :url
83
+ link_to(value, value, title: options[:title])
84
+ else
85
+ value.to_s
86
+ end
87
+ end
74
88
 
75
- if options[:icon]
76
- content = fa_icon(options[:icon], text: content)
89
+ def wrap_content(label, value, options)
90
+ label_html_options = {}
91
+ label_html_options = options.delete(:label_html) if options.key?(:label_html)
92
+
93
+ value_html_options = {}
94
+ value_html_options = options.delete(:value_html) if options.key?(:value_html)
95
+
96
+ content = content_tag(
97
+ wrappers[:value],
98
+ value,
99
+ apply_default_options(:value_html, value_html_options)
100
+ )
101
+
102
+ unless options[:label] === false
103
+ content = content_tag(
104
+ wrappers[:label],
105
+ "#{label}:",
106
+ apply_default_options(:label_html, label_html_options)
107
+ ) + ' ' + content
77
108
  end
78
109
 
110
+ content = fa_icon(options[:icon], text: content) if options[:icon]
79
111
  content
80
112
  end
81
113
 
114
+ def apply_default_options(key, options)
115
+ if default_options.key?(key)
116
+ default_options[key].merge(options)
117
+ else
118
+ options
119
+ end
120
+ end
121
+
82
122
  def human_name(attribute)
83
- object.class.human_attribute_name(attribute, default: attribute.to_s.titleize)
123
+ object
124
+ .class
125
+ .human_attribute_name(attribute, default: attribute.to_s.titleize)
84
126
  end
85
127
 
86
128
  def icon_map(method)
87
129
  {
88
130
  boolean: 'check',
89
131
  date: 'calendar',
90
- datetime: 'calendar',
132
+ datetime: 'calendar',
91
133
  duration: 'clock-o',
92
134
  email: 'envelope',
93
135
  phone: 'phone',
94
- url: 'globe',
136
+ url: 'globe'
95
137
  }[method]
96
138
  end
97
-
98
139
  end
99
140
  end
100
141
  end
@@ -1,3 +1,3 @@
1
1
  module AttributesFor
2
- VERSION = "0.6.1"
2
+ VERSION = "0.7.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attributes_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ole J. Rosendahl
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-29 00:00:00.000000000 Z
11
+ date: 2015-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails