data_attributes 1.1.0 → 1.1.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 +4 -4
- data/VERSION +1 -1
- data/data_attributes.gemspec +1 -2
- metadata +3 -21
- data/.gitignore +0 -7
- data/.rspec +0 -2
- data/Gemfile +0 -3
- data/Rakefile +0 -10
- data/spec/data_attributes/helper_spec.rb +0 -178
- data/spec/data_attributes/model_spec.rb +0 -125
- data/spec/spec_helper.rb +0 -26
- data/spec/support/mocks/article.rb +0 -15
- data/spec/support/mocks/category.rb +0 -7
- data/spec/support/mocks/content.rb +0 -7
- data/spec/support/mocks/view.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5463274c1fe6e5092190403ab6608252defa2f67
|
4
|
+
data.tar.gz: 14971214b1a924b1b39403812566caf2c853b0bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf8318017c62c406cb882a69c63a29d8e3d0bc282082a73f169f76355ba19127e75a57c431d7714456fbffa0d42eb2c7f0e1268ca97710fa1697266831fbe526
|
7
|
+
data.tar.gz: c4d8f4f88048c7fafc1f3332998f1c05814d206619f20bf730c309fdede0a65f851c2605abe8d954645d7ff1380dc6664c879676386ba5b4b4781c7e578f9850
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.1
|
data/data_attributes.gemspec
CHANGED
@@ -9,8 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.description = 'A gem to provide HTML data attributes from model to view'
|
10
10
|
s.license = 'MIT'
|
11
11
|
|
12
|
-
s.files = `git ls-files`.split("\n")
|
13
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
12
|
+
s.files = `git ls-files | grep -vE '^(spec/|test/|\\.|Gemfile|Rakefile)'`.split("\n")
|
14
13
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
15
14
|
s.require_paths = ['lib']
|
16
15
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexis Toulotte
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -96,25 +96,14 @@ executables: []
|
|
96
96
|
extensions: []
|
97
97
|
extra_rdoc_files: []
|
98
98
|
files:
|
99
|
-
- ".gitignore"
|
100
|
-
- ".rspec"
|
101
|
-
- Gemfile
|
102
99
|
- MIT-LICENSE
|
103
100
|
- README.mdown
|
104
|
-
- Rakefile
|
105
101
|
- VERSION
|
106
102
|
- data_attributes.gemspec
|
107
103
|
- lib/data_attributes.rb
|
108
104
|
- lib/data_attributes/helper.rb
|
109
105
|
- lib/data_attributes/model.rb
|
110
106
|
- lib/spec_helper.rb
|
111
|
-
- spec/data_attributes/helper_spec.rb
|
112
|
-
- spec/data_attributes/model_spec.rb
|
113
|
-
- spec/spec_helper.rb
|
114
|
-
- spec/support/mocks/article.rb
|
115
|
-
- spec/support/mocks/category.rb
|
116
|
-
- spec/support/mocks/content.rb
|
117
|
-
- spec/support/mocks/view.rb
|
118
107
|
homepage: https://github.com/alexistoulotte/data_attributes
|
119
108
|
licenses:
|
120
109
|
- MIT
|
@@ -139,11 +128,4 @@ rubygems_version: 2.5.2
|
|
139
128
|
signing_key:
|
140
129
|
specification_version: 4
|
141
130
|
summary: Provides HTML data attributes from model to view
|
142
|
-
test_files:
|
143
|
-
- spec/data_attributes/helper_spec.rb
|
144
|
-
- spec/data_attributes/model_spec.rb
|
145
|
-
- spec/spec_helper.rb
|
146
|
-
- spec/support/mocks/article.rb
|
147
|
-
- spec/support/mocks/category.rb
|
148
|
-
- spec/support/mocks/content.rb
|
149
|
-
- spec/support/mocks/view.rb
|
131
|
+
test_files: []
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
@@ -1,178 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe DataAttributes::Helper do
|
4
|
-
|
5
|
-
let(:view) { View.new }
|
6
|
-
|
7
|
-
describe '#data_attribute_value' do
|
8
|
-
|
9
|
-
it 'raise an error if given object is not supported' do
|
10
|
-
expect {
|
11
|
-
view.data_attribute_value(Object.new)
|
12
|
-
}.to raise_error("Can't convert object of class Object in data attributes")
|
13
|
-
end
|
14
|
-
|
15
|
-
context 'with string' do
|
16
|
-
|
17
|
-
it 'returns given value' do
|
18
|
-
expect(view.data_attribute_value('hello world')).to eq('hello world')
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'accepts :prefix_strings option' do
|
22
|
-
expect(view.data_attribute_value('hello world', prefix_strings: true)).to eq('string:hello world')
|
23
|
-
expect(view.data_attribute_value('hello world', prefix_strings: false)).to eq('hello world')
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'with symbol' do
|
29
|
-
|
30
|
-
it 'returns given value' do
|
31
|
-
expect(view.data_attribute_value(:hello)).to eq('hello')
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'accepts :prefix_strings option' do
|
35
|
-
expect(view.data_attribute_value(:hello, prefix_strings: true)).to eq('string:hello')
|
36
|
-
expect(view.data_attribute_value(:hello, prefix_strings: false)).to eq('hello')
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
context 'with integer' do
|
42
|
-
|
43
|
-
it 'returns given value' do
|
44
|
-
expect(view.data_attribute_value(42)).to eq(42)
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
context 'with float' do
|
50
|
-
|
51
|
-
it 'returns given value' do
|
52
|
-
expect(view.data_attribute_value(42.28)).to eq(42.28)
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
context 'with nil' do
|
58
|
-
|
59
|
-
it 'returns nil' do
|
60
|
-
expect(view.data_attribute_value(nil)).to be(nil)
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
context 'with Time' do
|
66
|
-
|
67
|
-
it 'returns time as integer' do
|
68
|
-
expect(view.data_attribute_value(Time.new(2017, 2, 10, 4, 1, 2))).to eq(1486659662)
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
context 'with true' do
|
74
|
-
|
75
|
-
it 'returns "true"' do
|
76
|
-
expect(view.data_attribute_value(true)).to eq('true')
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'accepts :raw option' do
|
80
|
-
expect(view.data_attribute_value(true, raw: true)).to eq(true)
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
84
|
-
|
85
|
-
context 'with false' do
|
86
|
-
|
87
|
-
it 'returns "false"' do
|
88
|
-
expect(view.data_attribute_value(false)).to eq('false')
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'accepts :raw option' do
|
92
|
-
expect(view.data_attribute_value(false, raw: true)).to eq(false)
|
93
|
-
end
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
context 'with array' do
|
98
|
-
|
99
|
-
it 'returns array as JSON' do
|
100
|
-
expect(view.data_attribute_value([42, :bam, true, Time.new(2017, 2, 10, 4, 1, 2)])).to eq('[42,"bam",true,1486659662]')
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'accepts :prefix_strings option' do
|
104
|
-
expect(view.data_attribute_value([42, :bam, true, Time.new(2017, 2, 10, 4, 1, 2)], prefix_strings: true)).to eq('[42,"string:bam",true,1486659662]')
|
105
|
-
end
|
106
|
-
|
107
|
-
it 'is correct with arrays of arrays' do
|
108
|
-
expect(view.data_attribute_value([42, [:bam, true]])).to eq('[42,["bam",true]]')
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'is correct with arrays of hashes' do
|
112
|
-
expect(view.data_attribute_value([44, { '_category_id?' => 28 }])).to eq('[44,{"categoryId":28}]')
|
113
|
-
end
|
114
|
-
|
115
|
-
end
|
116
|
-
|
117
|
-
context 'with hash' do
|
118
|
-
|
119
|
-
it 'returns hash as JSON' do
|
120
|
-
expect(view.data_attribute_value({ id: 42, 'message' => :bam, date: Time.new(2017, 2, 10, 4, 1, 2), valid: true })).to eq('{"id":42,"message":"bam","date":1486659662,"valid":true}')
|
121
|
-
end
|
122
|
-
|
123
|
-
it 'accepts :prefix_strings option' do
|
124
|
-
expect(view.data_attribute_value({ id: 42, 'message' => :bam, date: Time.new(2017, 2, 10, 4, 1, 2), valid: true }, prefix_strings: true)).to eq('{"id":42,"message":"string:bam","date":1486659662,"valid":true}')
|
125
|
-
end
|
126
|
-
|
127
|
-
it 'converts keys correctly' do
|
128
|
-
expect(view.data_attribute_value({ '_category_id?' => 28 })).to eq('{"categoryId":28}')
|
129
|
-
end
|
130
|
-
|
131
|
-
end
|
132
|
-
|
133
|
-
context 'with a date' do
|
134
|
-
|
135
|
-
it 'returns date formatted' do
|
136
|
-
expect(view.data_attribute_value(Date.new(2017, 4, 27))).to eq('2017/04/27')
|
137
|
-
end
|
138
|
-
|
139
|
-
end
|
140
|
-
|
141
|
-
context 'with a data attributes model' do
|
142
|
-
|
143
|
-
it 'returns hash of attributes as JSON' do
|
144
|
-
Article.data_attribute(:id, :body)
|
145
|
-
article = Article.new
|
146
|
-
article.body = 'This is the body'
|
147
|
-
article.id = 28
|
148
|
-
expect(view.data_attribute_value(article)).to eq('{"body":"This is the body","id":28}')
|
149
|
-
end
|
150
|
-
|
151
|
-
it 'accepts :prefix_strings option' do
|
152
|
-
Article.data_attribute(:id, :body)
|
153
|
-
article = Article.new
|
154
|
-
article.body = 'This is the body'
|
155
|
-
article.id = 28
|
156
|
-
expect(view.data_attribute_value(article, prefix_strings: true)).to eq('{"body":"string:This is the body","id":28}')
|
157
|
-
end
|
158
|
-
|
159
|
-
it 'is correct with a nested data attributes object' do
|
160
|
-
Article.data_attribute(:id, :body, :category)
|
161
|
-
Category.data_attribute(:id, :title)
|
162
|
-
|
163
|
-
category = Category.new
|
164
|
-
category.id = 20
|
165
|
-
category.title = 'Books'
|
166
|
-
|
167
|
-
article = Article.new
|
168
|
-
article.body = 'This is the body'
|
169
|
-
article.category = category
|
170
|
-
|
171
|
-
expect(view.data_attribute_value(article)).to eq('{"body":"This is the body","category":{"id":20,"title":"Books"},"id":null}')
|
172
|
-
end
|
173
|
-
|
174
|
-
end
|
175
|
-
|
176
|
-
end
|
177
|
-
|
178
|
-
end
|
@@ -1,125 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe DataAttributes::Model do
|
4
|
-
|
5
|
-
describe '.data_attribute' do
|
6
|
-
|
7
|
-
it 'adds given attributes to data attributes' do
|
8
|
-
expect {
|
9
|
-
Article.data_attribute(:id)
|
10
|
-
}.to change { Article.data_attributes }.from([]).to([:id])
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'removes doubloons' do
|
14
|
-
Article.data_attribute(:id)
|
15
|
-
expect {
|
16
|
-
Article.data_attribute(:id)
|
17
|
-
}.not_to change { Article.data_attributes }
|
18
|
-
expect(Article.send(:__data_attributes)).to eq([:id])
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'removes doubloons (if given as string)' do
|
22
|
-
Article.data_attribute(:id)
|
23
|
-
expect {
|
24
|
-
Article.data_attribute('id')
|
25
|
-
}.not_to change { Article.data_attributes }
|
26
|
-
expect(Article.send(:__data_attributes)).to eq([:id])
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'converts attributes to symbols' do
|
30
|
-
Article.data_attribute('id')
|
31
|
-
expect(Article.data_attributes).to eq([:id])
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'accepts arrays' do
|
35
|
-
Article.data_attribute(['id', ['category']], :body)
|
36
|
-
expect(Article.data_attributes).to eq([:body, :category, :id])
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
describe '.__data_attributes' do
|
42
|
-
|
43
|
-
it 'is a private method' do
|
44
|
-
expect(Article.respond_to?(:__data_attributes)).to be(false)
|
45
|
-
expect(Article.respond_to?(:__data_attributes, true)).to be(true)
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'is a private method even after a call to .data_attribute' do
|
49
|
-
Article.data_attribute(:id)
|
50
|
-
expect(Article.respond_to?(:__data_attributes)).to be(false)
|
51
|
-
expect(Article.respond_to?(:__data_attributes, true)).to be(true)
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
describe '.data_attributes' do
|
57
|
-
|
58
|
-
it 'is an empty array by default' do
|
59
|
-
expect(Article.data_attributes).to eq([])
|
60
|
-
end
|
61
|
-
|
62
|
-
it "can't be changed" do
|
63
|
-
Article.data_attribute(:id)
|
64
|
-
expect {
|
65
|
-
Article.data_attributes.clear
|
66
|
-
}.not_to change { Article.data_attribute }
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'is correct with subclasses' do
|
70
|
-
Content.data_attribute(:body, :id)
|
71
|
-
expect(Content.data_attributes).to eq([:body, :id])
|
72
|
-
expect(Article.data_attributes).to eq([:body, :id])
|
73
|
-
|
74
|
-
Article.data_attribute(:title)
|
75
|
-
expect(Content.data_attributes).to eq([:body, :id])
|
76
|
-
expect(Article.data_attributes).to eq([:body, :id, :title])
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'removes doubloons with subclasses' do
|
80
|
-
Content.data_attribute(:body, :id)
|
81
|
-
expect(Article.data_attributes).to eq([:body, :id])
|
82
|
-
Article.data_attribute(:title, :id, :body)
|
83
|
-
expect(Article.data_attributes).to eq([:body, :id, :title])
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'is sorted' do
|
87
|
-
Article.data_attribute(:id, :body)
|
88
|
-
expect(Article.data_attributes).to eq([:body, :id])
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'is sorted with subclasses' do
|
92
|
-
Content.data_attribute(:bar, :foo)
|
93
|
-
Article.data_attribute(:body)
|
94
|
-
expect(Article.data_attributes).to eq([:bar, :body, :foo])
|
95
|
-
end
|
96
|
-
|
97
|
-
end
|
98
|
-
|
99
|
-
describe '#data_attributes' do
|
100
|
-
|
101
|
-
it 'returns an hash of instance data attributes' do
|
102
|
-
Article.data_attribute(:id, :body)
|
103
|
-
article = Article.new
|
104
|
-
article.body = 'This is the body'
|
105
|
-
article.id = 42
|
106
|
-
expect(article.data_attributes).to eq({ body: 'This is the body', id: 42 })
|
107
|
-
end
|
108
|
-
|
109
|
-
it 'does not fail if method does not exist' do
|
110
|
-
Article.data_attribute(:id, :foo)
|
111
|
-
article = Article.new
|
112
|
-
article.id = 42
|
113
|
-
expect(article.data_attributes).to eq({ id: 42 })
|
114
|
-
end
|
115
|
-
|
116
|
-
it 'does not fail if method is private' do
|
117
|
-
Article.data_attribute(:published)
|
118
|
-
article = Article.new
|
119
|
-
article.published = true
|
120
|
-
expect(article.data_attributes).to eq({ published: true })
|
121
|
-
end
|
122
|
-
|
123
|
-
end
|
124
|
-
|
125
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require File.expand_path("#{__dir__}/../lib/data_attributes")
|
2
|
-
require 'byebug'
|
3
|
-
|
4
|
-
# Support
|
5
|
-
require "#{__dir__}/support/mocks/content"
|
6
|
-
require "#{__dir__}/support/mocks/article"
|
7
|
-
require "#{__dir__}/support/mocks/category"
|
8
|
-
require "#{__dir__}/support/mocks/view"
|
9
|
-
|
10
|
-
RSpec.configure do |config|
|
11
|
-
config.raise_errors_for_deprecations!
|
12
|
-
|
13
|
-
config.before(:each) do
|
14
|
-
[Article, Category, Content].each do |mock_class|
|
15
|
-
mock_class.class_eval(%Q{
|
16
|
-
class << self
|
17
|
-
private
|
18
|
-
|
19
|
-
def __data_attributes
|
20
|
-
[]
|
21
|
-
end
|
22
|
-
end
|
23
|
-
})
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|