attributes_for 0.2.0 → 0.3.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fbe5caa06296a883ff1be0b0aef988668c71c94d
4
- data.tar.gz: 446b2c7ea7b75df3a1d477ea6188e9498a0381a3
3
+ metadata.gz: f23d4cb89d3cc588d8e7581eac7ffd8c5abdf4a4
4
+ data.tar.gz: dfdfb77f005a66815d07c009ff6f89ccfa460e80
5
5
  SHA512:
6
- metadata.gz: 562b36098e0dcd9f39ef1c447246ab7c6e1153ced1c003f9ca19cf6cad93201d4801a16ab801637895af587bd5192da307015394c7158f80c9c0989d8310e25a
7
- data.tar.gz: 40af89eccc208a8f45e3a8ab303f064aae50cb5a586f6bba7a5f4148d5c48bfb38fb73fdc53f98c220c5bdc954486528be353977438c0286f79e02fb12ef6ae5
6
+ metadata.gz: 7d76570dcf17f8dabfd00aa2eb6ad2317c6c1066b24dfb1ae7afb292e24e5331d1d58a78eb25fcb2faeae8243b75088d2621bbce5dc4097907a962dc69c6e36e
7
+ data.tar.gz: c9db4d9229d1a389a44a5a60d0e4a78d1a3298ad88a52bf58007082feb3b568f2d7d0627ed7ec46975ae2b1dc78883e76f156e11bbde513b3f06f8b258a9abe8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.3.1] - 2015-08-06
4
+ ### Fixed
5
+ - Fixed stylesheet loading (insert 'require' during installation).
6
+
7
+ ## [0.3.0] - 2015-08-05
8
+ ### Added
9
+ - All formatting methods now takes a block. The contents of the block
10
+ will be displayed after the label.
11
+
3
12
  ## [0.2.0] - 2015-08-04
4
13
  ### Added
5
14
  - `boolean` formatting helper, will show 'Yes' or 'No' depending on
data/README.md CHANGED
@@ -23,22 +23,33 @@ Or install it yourself as:
23
23
 
24
24
  $ gem install attributes_for
25
25
 
26
+ Run generator to add I18n locales:
27
+
28
+ $ rails generate attributes_for:install
29
+
30
+ ## Screenshot
31
+ ![Screenshot](https://cloud.githubusercontent.com/assets/1222916/9083171/fac21df2-3b92-11e5-837a-71e70ee24cdd.png)
32
+
26
33
  ## Usage
27
34
 
28
35
  Present the attributes using the API in the example below. It can also generate a
29
- standard label value pair given an attribute name using the `attr` method. Arbitrary
36
+ standard label value pair given an attribute name using the `attribute` method. Arbitrary
30
37
  strings can also be presented using the `string` method.
31
38
 
32
39
  ```ruby
33
- <ul class="unstyled">
40
+ <ul class="list-unstyled">
34
41
  <%= attributes_for @company do |b| %>
42
+ <li><%= b.attribute :name, class: 'fa fa-building-o' %></li>
35
43
  <li><%= b.phone :phone %></li>
36
44
  <li><%= b.email :email %></li>
37
45
  <li><%= b.url :website %></li>
38
- <li><%= b.date :created_at %></li>
39
- <li><%= b.attribute :credit_rating %></li>
46
+ <li>
47
+ <%= b.attribute(:user, class: 'fa fa-user') do %>
48
+ <%= link_to @company.user_name, url_for(@company.user) %>
49
+ <% end %>
50
+ </li>
40
51
  <li><%= b.boolean :active %></li>
41
- <li><%= b.string "A string" %></li>
52
+ <li><%= b.date :created_at, format: :long %> </li>
42
53
  <% end %>
43
54
  </ul>
44
55
  ```
@@ -0,0 +1,3 @@
1
+ /*
2
+ *= require font-awesome
3
+ */
@@ -2,88 +2,86 @@ module AttributesFor
2
2
  module AttributesForHelper
3
3
 
4
4
  def attributes_for(object, options = {}, &block)
5
- builder = AttributeBuilder.new(object)
5
+ builder = AttributeBuilder.new(object, self)
6
6
  capture builder, &block
7
7
  end
8
8
 
9
9
  class AttributeBuilder
10
10
  include ActionView::Helpers
11
11
 
12
- attr_accessor :object, :output_buffer
12
+ attr_accessor :object, :template, :output_buffer
13
13
 
14
- def initialize(object)
15
- @object = object
14
+ def initialize(object, template)
15
+ @object, @template = object, template
16
16
  end
17
17
 
18
- def boolean(attribute_name, options = {})
18
+ def boolean(attribute_name, options = {}, &block)
19
19
  options[:class] ||= 'fa fa-check'
20
-
21
- content(attribute_name, options) do |value|
22
- I18n.t("attributes_for.#{value.to_s}")
23
- end
20
+ content(attribute_name, __method__, options, &block)
24
21
  end
25
22
 
26
- def phone(attribute_name, options = {})
23
+ def phone(attribute_name, options = {}, &block)
27
24
  options[:class] ||= 'fa fa-phone'
28
-
29
- content(attribute_name, options) do |value|
30
- link_to(" #{number_to_phone(value)}", "tel:#{value}", title: human_name(attribute_name))
31
- end
25
+ content(attribute_name, __method__, options, &block)
32
26
  end
33
27
 
34
- def email(attribute_name, options = {})
28
+ def email(attribute_name, options = {}, &block)
35
29
  options[:class] ||= 'fa fa-envelope'
36
-
37
- content(attribute_name, options) do |value|
38
- mail_to(" #{value}", value, title: human_name(attribute_name))
39
- end
30
+ content(attribute_name, __method__, options, &block)
40
31
  end
41
32
 
42
- def url(attribute_name, options = {})
33
+ def url(attribute_name, options = {}, &block)
43
34
  options[:class] ||= 'fa fa-globe'
44
-
45
- content(attribute_name, options) do |value|
46
- link_to(" #{value}", value, title: human_name(attribute_name))
47
- end
35
+ content(attribute_name, __method__, options, &block)
48
36
  end
49
37
 
50
- def date(attribute_name, options = {})
38
+ def date(attribute_name, options = {}, &block)
51
39
  options[:class] ||= 'fa fa-clock-o'
52
-
53
- content(attribute_name, options) do |value|
54
- I18n.l(value, format: options[:format])
55
- end
40
+ content(attribute_name, __method__, options, &block)
56
41
  end
57
42
 
58
43
  def string(content, options = {})
59
44
  wrap_content(" #{content}", options)
60
45
  end
61
46
 
62
- def attribute(attribute_name, options = {})
63
- content(attribute_name, options)
47
+ def attribute(attribute_name, options = {}, &block)
48
+ content(attribute_name, __method__, options, &block)
64
49
  end
65
50
 
66
51
  private
67
52
 
68
- def content(attribute_name, options = {}, &block)
69
- options[:id] ||= attribute_name.to_s
70
-
71
- wrap_content(
72
- label(attribute_name, options) + prepare_value(attribute_value(attribute_name), &block),
73
- options
74
- )
53
+ def content(attribute_name, method, options = {}, &block)
54
+ wrap_content(label(attribute_name, options) + build_content(attribute_name, method, options, &block), options)
75
55
  end
76
56
 
77
- def prepare_value(value, &block)
78
- if value.to_s.empty?
79
- I18n.t "attributes_for.not_set"
80
- elsif block_given?
81
- yield value
82
- else
57
+ def build_content(attribute_name, method, options = {}, &block)
58
+ return template.capture(&block) if block_given?
59
+
60
+ value = attribute_value(attribute_name)
61
+
62
+ return I18n.t "attributes_for.not_set" if value.to_s.empty?
63
+
64
+ case method.to_sym
65
+ when :attribute
83
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))
84
77
  end
85
78
  end
86
79
 
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)
83
+ end
84
+
87
85
  def wrap_content(content, options = {})
88
86
  content_tag(:i, content, id: options[:id], class: options[:class])
89
87
  end
@@ -1,3 +1,5 @@
1
1
  module AttributesFor
2
- class Engine < Rails::Engine; end
2
+ class Engine < Rails::Engine
3
+ require 'font-awesome-rails'
4
+ end
3
5
  end
@@ -1,3 +1,3 @@
1
1
  module AttributesFor
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -8,6 +8,13 @@ module AttributesFor
8
8
  copy_file 'attributes_for.en.yml', 'config/locales/attributes_for.en.yml'
9
9
  end
10
10
 
11
+ def inject_stylesheets
12
+ file = "app/assets/stylesheets/application.css"
13
+
14
+ if File.exists?(Rails.root.join(file))
15
+ inject_into_file file, " *= require attributes_for\n", {before: %r{.*require_self}}
16
+ end
17
+ end
11
18
  end
12
19
  end
13
20
  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.2.0
4
+ version: 0.3.1
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-08-04 00:00:00.000000000 Z
11
+ date: 2015-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -96,6 +96,7 @@ files:
96
96
  - LICENSE.txt
97
97
  - README.md
98
98
  - Rakefile
99
+ - app/assets/stylesheets/attributes_for.css
99
100
  - app/helpers/attributes_for/attributes_for_helper.rb
100
101
  - attributes_for.gemspec
101
102
  - bin/console