dom_for 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +5 -5
- data/lib/dom_for.rb +4 -3
- data/lib/dom_for/model.rb +6 -3
- data/lib/dom_for/record.rb +5 -3
- data/lib/dom_for/version.rb +1 -1
- data/spec/helpers/dom_for_spec.rb +4 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff2f6bc3ad37ace7fcf626962600351cfb257088
|
4
|
+
data.tar.gz: ab0d28b29b3b99c5977414e5192c55d285d70474
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18bbd0f94835183e60dd520c573d6dd1de354608f9ebdfb5b8e4c010cb5bc6338e6ee865e626198cc168f206f66a670f11f591b8f18c7c481219bfae4a586321
|
7
|
+
data.tar.gz: 2a81fa2105bedbfe31a65708160d68189b0441967a01f80bda3be72361b71c1939a9ecbb73f22e4846893a394ed0879b4d53a7acaae34344224b75fa16f58e8a
|
data/CHANGELOG.md
CHANGED
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:
|
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
|
```
|
data/lib/dom_for.rb
CHANGED
@@ -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
|
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
|
data/lib/dom_for/model.rb
CHANGED
@@ -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:
|
84
|
+
content_tag(tag, id: object_id, class: html_class, data: attrs, &block)
|
82
85
|
else
|
83
|
-
tag(tag, id: object_id, class:
|
86
|
+
tag(tag, id: object_id, class: html_class, data: attrs)
|
84
87
|
end
|
85
88
|
rescue
|
86
89
|
content_tag(tag, &block)
|
data/lib/dom_for/record.rb
CHANGED
@@ -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:
|
43
|
+
content_tag(tag, id: object_id, class: html_class, data: attrs, &block)
|
42
44
|
else
|
43
|
-
tag(tag, id: object_id, class:
|
45
|
+
tag(tag, id: object_id, class: html_class, data: attrs)
|
44
46
|
end
|
45
47
|
rescue
|
46
48
|
content_tag(tag, &block)
|
data/lib/dom_for/version.rb
CHANGED
@@ -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" />'
|