attributes_for 0.3.1 → 0.3.2

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: f23d4cb89d3cc588d8e7581eac7ffd8c5abdf4a4
4
- data.tar.gz: dfdfb77f005a66815d07c009ff6f89ccfa460e80
3
+ metadata.gz: 24cac71563fed75d672dc6bfc93c1126ba39060b
4
+ data.tar.gz: c3da138c954270275ce2a2f36210e6d3969abd2b
5
5
  SHA512:
6
- metadata.gz: 7d76570dcf17f8dabfd00aa2eb6ad2317c6c1066b24dfb1ae7afb292e24e5331d1d58a78eb25fcb2faeae8243b75088d2621bbce5dc4097907a962dc69c6e36e
7
- data.tar.gz: c9db4d9229d1a389a44a5a60d0e4a78d1a3298ad88a52bf58007082feb3b568f2d7d0627ed7ec46975ae2b1dc78883e76f156e11bbde513b3f06f8b258a9abe8
6
+ metadata.gz: 0d7c590348937bae74c81560f490d52b59e5483d7ab9a5cf59bdbebce090a7443b7ba958e96dd1d1f21c4bdf735a3c04f292738bb3babce826b37e2771ab1264
7
+ data.tar.gz: 7aac3470f57f602bd38284643ae72e20c790bf00ba47ac0cbfbef9490feeb33cfc82e62dd45db5ac646803bd3b74a215e68fc615e2a2e4950bc231cbad433df1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.3.2] - 2015-08-06
4
+ ### Changed
5
+ - Rewrote implementation using method_missing
6
+
3
7
  ## [0.3.1] - 2015-08-06
4
8
  ### Fixed
5
9
  - Fixed stylesheet loading (insert 'require' during installation).
@@ -15,71 +15,44 @@ module AttributesFor
15
15
  @object, @template = object, template
16
16
  end
17
17
 
18
- def boolean(attribute_name, options = {}, &block)
19
- options[:class] ||= 'fa fa-check'
20
- content(attribute_name, __method__, options, &block)
21
- end
22
-
23
- def phone(attribute_name, options = {}, &block)
24
- options[:class] ||= 'fa fa-phone'
25
- content(attribute_name, __method__, options, &block)
26
- end
27
-
28
- def email(attribute_name, options = {}, &block)
29
- options[:class] ||= 'fa fa-envelope'
30
- content(attribute_name, __method__, options, &block)
31
- end
32
-
33
- def url(attribute_name, options = {}, &block)
34
- options[:class] ||= 'fa fa-globe'
35
- content(attribute_name, __method__, options, &block)
36
- end
37
-
38
- def date(attribute_name, options = {}, &block)
39
- options[:class] ||= 'fa fa-clock-o'
40
- content(attribute_name, __method__, options, &block)
41
- end
42
-
43
18
  def string(content, options = {})
44
19
  wrap_content(" #{content}", options)
45
20
  end
46
21
 
47
- def attribute(attribute_name, options = {}, &block)
48
- content(attribute_name, __method__, options, &block)
22
+ def method_missing(m, *args, &block)
23
+ build_content(m, *args, &block)
49
24
  end
50
25
 
51
26
  private
52
27
 
53
- def content(attribute_name, method, options = {}, &block)
54
- wrap_content(label(attribute_name, options) + build_content(attribute_name, method, options, &block), options)
55
- end
56
-
57
- def build_content(attribute_name, method, options = {}, &block)
58
- return template.capture(&block) if block_given?
59
-
28
+ def build_content(method, attribute_name, options = {}, &block)
60
29
  value = attribute_value(attribute_name)
61
30
 
62
- return I18n.t "attributes_for.not_set" if value.to_s.empty?
63
-
64
- case method.to_sym
65
- when :attribute
66
- value
67
- when :boolean
68
- I18n.t("attributes_for.#{value.to_s}")
69
- when :date
70
- I18n.l(value, format: options[:format])
71
- when :email
72
- mail_to(" #{value}", value, title: human_name(attribute_name))
73
- when :phone
74
- link_to(" #{number_to_phone(value)}", "tel:#{value}", title: human_name(attribute_name))
75
- when :url
76
- link_to(" #{value}", value, title: human_name(attribute_name))
31
+ content = if block_given?
32
+ template.capture(&block)
33
+ elsif value.to_s.empty?
34
+ I18n.t "attributes_for.not_set"
35
+ else
36
+ case method.to_sym
37
+ when :boolean
38
+ I18n.t("attributes_for.#{value.to_s}")
39
+ when :date
40
+ I18n.l(value, format: options[:format])
41
+ when :email
42
+ mail_to(" #{value}", value, title: human_name(attribute_name))
43
+ when :phone
44
+ link_to(" #{number_to_phone(value)}", "tel:#{value}", title: human_name(attribute_name))
45
+ when :url
46
+ link_to(" #{value}", value, title: human_name(attribute_name))
47
+ else
48
+ value
49
+ end
77
50
  end
78
- end
79
51
 
80
- def content(attribute_name, method, options = {}, &block)
81
- options[:id] ||= attribute_name.to_s
82
- wrap_content(label(attribute_name, options) + build_content(attribute_name, method, options, &block), options)
52
+ options[:class] ||= icon_map(method)
53
+ options[:id] ||= attribute_name.to_s
54
+
55
+ wrap_content(label(attribute_name, options) + content, options)
83
56
  end
84
57
 
85
58
  def wrap_content(content, options = {})
@@ -103,6 +76,16 @@ module AttributesFor
103
76
  object.class.human_attribute_name(attribute)
104
77
  end
105
78
 
79
+ def icon_map(method)
80
+ {
81
+ boolean: 'fa fa-check',
82
+ date: 'fa fa-clock-o',
83
+ email: 'fa fa-envelope',
84
+ phone: 'fa fa-phone',
85
+ url: 'fa fa-globe',
86
+ }[method]
87
+ end
88
+
106
89
  end
107
90
  end
108
91
  end
@@ -1,3 +1,3 @@
1
1
  module AttributesFor
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attributes_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ole J. Rosendahl