dom_for 1.1.0 → 1.2.0

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: b8fc0fad1bfddcb6a68a0801745603b5d4a4aed9
4
- data.tar.gz: 6c713b8e9135fb37737a3c5dd56da9dc05b5b09a
3
+ metadata.gz: ff2f6bc3ad37ace7fcf626962600351cfb257088
4
+ data.tar.gz: ab0d28b29b3b99c5977414e5192c55d285d70474
5
5
  SHA512:
6
- metadata.gz: 22b9ee7cb04d7c2c19328533812d64bfe486d7a687333cb43c6e52e422e15c58f445877a374b164b8b12b4d384704001cddd8ca2917b6e2ccbf271d2c26ac2b1
7
- data.tar.gz: 001723acf36b81251b7ac16a9c266fc2c31863f449ad06a10131f1c8ec80839d9d2949cc1bdd0b843323ae1aee0d0c5cb18d4d7b95beb720f7f291f0d368dc48
6
+ metadata.gz: 18bbd0f94835183e60dd520c573d6dd1de354608f9ebdfb5b8e4c010cb5bc6338e6ee865e626198cc168f206f66a670f11f591b8f18c7c481219bfae4a586321
7
+ data.tar.gz: 2a81fa2105bedbfe31a65708160d68189b0441967a01f80bda3be72361b71c1939a9ecbb73f22e4846893a394ed0879b4d53a7acaae34344224b75fa16f58e8a
@@ -1,3 +1,9 @@
1
+ ## 1.2.0 (24/11/2015)
2
+
3
+ ### Features
4
+
5
+ * Ability to passing html class as an argument
6
+
1
7
  ## 1.1.0 (24/11/2015)
2
8
 
3
9
  ### Features
data/README.md CHANGED
@@ -24,11 +24,11 @@ Or install it yourself as:
24
24
 
25
25
  ## Usage
26
26
 
27
- By default the helper `dom_for` creates tag `div`. But it can override, passing an additional argument `tag`, for example: `dom _for User, tag: :span`:
27
+ By default the helper `dom_for` creates tag `div`. But it can override, passing an additional argument `tag: :span`, for example:
28
28
 
29
29
  ```erb
30
30
  <%# /app/views/users/index.html.erb %>
31
- <%= dom_for User, attribute_1: 'value_1', attribute_2: 'value_2', attribute_3: 'value_3' do %>
31
+ <%= dom_for User, class: 'customers', attribute_1: 'value_1', attribute_2: 'value_2', attribute_3: 'value_3' do %>
32
32
  <% @users.each do |user| %>
33
33
  <%= dom_for user, tag: :p, admin: user.admin, blocked: user.blocked do %>
34
34
  <%= content_tag(:span, user.name) %>
@@ -38,7 +38,7 @@ By default the helper `dom_for` creates tag `div`. But it can override, passing
38
38
  ```
39
39
 
40
40
  ```html
41
- <div id="users" class="users" data-action="index" data-attribute-1="value_1" data-attribute-2="value_2" data-attribute-3="value_3">
41
+ <div id="users" class="users customers" data-action="index" data-attribute-1="value_1" data-attribute-2="value_2" data-attribute-3="value_3">
42
42
  <p id="user_1" class="user" data-admin="true" data-blocked="false" data-object-id="1">
43
43
  <span>Mikhail</span>
44
44
  </div>
@@ -82,13 +82,13 @@ The second argument passed to additional html-attributes (is optional):
82
82
 
83
83
  ```erb
84
84
  <%# /app/views/users/index.html.erb %>
85
- <%= dom_for User, attribute_1: 'value_1', attribute_2: 'value_2' do %>
85
+ <%= dom_for User class: 'clients', attribute_1: 'value_1', attribute_2: 'value_2' do %>
86
86
  <%= tag(:span) %>
87
87
  <% end %>
88
88
  ```
89
89
 
90
90
  ```html
91
- <div id="users" class="users" data-action="index" data-attribute-1="value_1" data-attribute-2="value_2">
91
+ <div id="users" class="users clients" data-action="index" data-attribute-1="value_1" data-attribute-2="value_2">
92
92
  <span />
93
93
  </div>
94
94
  ```
@@ -28,12 +28,13 @@ module DomFor
28
28
  # @return [String] Sanitized HTML string
29
29
  #
30
30
  def dom_for(object, attrs = {}, &block)
31
- tag = attrs.delete(:tag) || :div
31
+ tag = attrs.delete(:tag) || :div
32
+ klass = attrs.delete(:class)
32
33
 
33
34
  if object.instance_of? Class
34
- _dom_for_model(object, tag, attrs, &block)
35
+ _dom_for_model(object, tag, klass, attrs, &block)
35
36
  else
36
- _dom_for_record(object, tag, attrs, &block)
37
+ _dom_for_record(object, tag, klass, attrs, &block)
37
38
  end
38
39
  end
39
40
  end
@@ -47,12 +47,13 @@ module DomFor
47
47
  #
48
48
  # @param klass [Class] Model of ActiveRecord::Base
49
49
  # @param tag [Symbol] HTML tag name
50
+ # @param user_class [String] HTML class
50
51
  # @param attrs [Hash] Additional attributes for the record
51
52
  # @param block [Proc] Block for a div tag
52
53
  #
53
54
  # @return [String] Sanitized HTML string
54
55
  #
55
- def _dom_for_model(klass, tag, attrs = {}, &block)
56
+ def _dom_for_model(klass, tag, user_class, attrs = {}, &block)
56
57
  object_classes = []
57
58
  class_name = klass.to_s.underscore
58
59
  request_action = request.path_parameters[:action]
@@ -77,10 +78,12 @@ module DomFor
77
78
  class_name.pluralize
78
79
  end
79
80
 
81
+ html_class = object_classes.push(user_class).compact
82
+
80
83
  if block_given?
81
- content_tag(tag, id: object_id, class: object_classes.join(' '), data: attrs, &block)
84
+ content_tag(tag, id: object_id, class: html_class, data: attrs, &block)
82
85
  else
83
- tag(tag, id: object_id, class: object_classes.join(' '), data: attrs)
86
+ tag(tag, id: object_id, class: html_class, data: attrs)
84
87
  end
85
88
  rescue
86
89
  content_tag(tag, &block)
@@ -26,21 +26,23 @@ module DomFor
26
26
  #
27
27
  # @param record [ActiveRecord::Base] Instance of ActiveRecord::Base
28
28
  # @param tag [Symbol] HTML tag name
29
+ # @param user_class [String] HTML class
29
30
  # @param attrs [Hash] Additional attributes for the record
30
31
  # @param block [Proc] Block for a div tag
31
32
  #
32
33
  # @return [String] Sanitized HTML string
33
34
  #
34
- def _dom_for_record(record, tag, attrs = {}, &block)
35
+ def _dom_for_record(record, tag, user_class, attrs = {}, &block)
35
36
  object_id = dom_id(record)
36
37
  object_class = dom_class(record.class)
38
+ html_class = [object_class, user_class].compact
37
39
 
38
40
  attrs = attrs.merge(object_id: record.id) if record.persisted?
39
41
 
40
42
  if block_given?
41
- content_tag(tag, id: object_id, class: object_class, data: attrs, &block)
43
+ content_tag(tag, id: object_id, class: html_class, data: attrs, &block)
42
44
  else
43
- tag(tag, id: object_id, class: object_class, data: attrs)
45
+ tag(tag, id: object_id, class: html_class, data: attrs)
44
46
  end
45
47
  rescue
46
48
  content_tag(tag, &block)
@@ -1,3 +1,3 @@
1
1
  module DomFor
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -10,6 +10,10 @@ describe DomFor do
10
10
  expect(helper.dom_for(User, tag: :span)).to eq '<span id="users" class="users" />'
11
11
  end
12
12
 
13
+ it 'returns div with html class' do
14
+ expect(helper.dom_for(User, class: 'test ruby')).to eq '<div id="users" class="users test ruby" />'
15
+ end
16
+
13
17
  context 'with model' do
14
18
  it 'returns div without nested tags' do
15
19
  expect(helper.dom_for(User)).to eq '<div id="users" class="users" />'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dom_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikhail Grachev