acts_as_metadata 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +53 -54
- data/README.md +61 -0
- data/acts_as_metadata.gemspec +1 -1
- data/lib/metadata/acts_as_metadata.rb +27 -5
- data/lib/metadata/version.rb +1 -1
- data/spec/acts_as_metadata_spec.rb +68 -4
- data/spec/spec_helper.rb +1 -0
- metadata +5 -5
- data/Readme +0 -11
data/Gemfile.lock
CHANGED
@@ -1,80 +1,78 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
acts_as_metadata (0.0
|
4
|
+
acts_as_metadata (0.1.0)
|
5
5
|
rails (>= 3.0.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
actionmailer (3.
|
11
|
-
actionpack (= 3.
|
12
|
-
mail (~> 2.
|
13
|
-
actionpack (3.
|
14
|
-
activemodel (= 3.
|
15
|
-
activesupport (= 3.
|
10
|
+
actionmailer (3.2.9)
|
11
|
+
actionpack (= 3.2.9)
|
12
|
+
mail (~> 2.4.4)
|
13
|
+
actionpack (3.2.9)
|
14
|
+
activemodel (= 3.2.9)
|
15
|
+
activesupport (= 3.2.9)
|
16
16
|
builder (~> 3.0.0)
|
17
17
|
erubis (~> 2.7.0)
|
18
|
-
|
19
|
-
rack (~> 1.
|
20
|
-
rack-cache (~> 1.
|
21
|
-
rack-mount (~> 0.8.2)
|
18
|
+
journey (~> 1.0.4)
|
19
|
+
rack (~> 1.4.0)
|
20
|
+
rack-cache (~> 1.2)
|
22
21
|
rack-test (~> 0.6.1)
|
23
|
-
sprockets (~> 2.
|
24
|
-
activemodel (3.
|
25
|
-
activesupport (= 3.
|
22
|
+
sprockets (~> 2.2.1)
|
23
|
+
activemodel (3.2.9)
|
24
|
+
activesupport (= 3.2.9)
|
26
25
|
builder (~> 3.0.0)
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
arel (~> 2.2.1)
|
26
|
+
activerecord (3.2.9)
|
27
|
+
activemodel (= 3.2.9)
|
28
|
+
activesupport (= 3.2.9)
|
29
|
+
arel (~> 3.0.2)
|
32
30
|
tzinfo (~> 0.3.29)
|
33
|
-
activeresource (3.
|
34
|
-
activemodel (= 3.
|
35
|
-
activesupport (= 3.
|
36
|
-
activesupport (3.
|
31
|
+
activeresource (3.2.9)
|
32
|
+
activemodel (= 3.2.9)
|
33
|
+
activesupport (= 3.2.9)
|
34
|
+
activesupport (3.2.9)
|
35
|
+
i18n (~> 0.6)
|
37
36
|
multi_json (~> 1.0)
|
38
|
-
arel (
|
39
|
-
builder (3.0.
|
37
|
+
arel (3.0.2)
|
38
|
+
builder (3.0.4)
|
40
39
|
diff-lcs (1.1.3)
|
41
40
|
erubis (2.7.0)
|
42
41
|
hike (1.2.1)
|
43
|
-
i18n (0.6.
|
44
|
-
|
45
|
-
|
42
|
+
i18n (0.6.1)
|
43
|
+
journey (1.0.4)
|
44
|
+
json (1.7.5)
|
45
|
+
mail (2.4.4)
|
46
46
|
i18n (>= 0.4.0)
|
47
47
|
mime-types (~> 1.16)
|
48
48
|
treetop (~> 1.4.8)
|
49
|
-
mime-types (1.
|
50
|
-
multi_json (1.
|
49
|
+
mime-types (1.19)
|
50
|
+
multi_json (1.3.7)
|
51
51
|
polyglot (0.3.3)
|
52
|
-
rack (1.
|
53
|
-
rack-cache (1.
|
52
|
+
rack (1.4.1)
|
53
|
+
rack-cache (1.2)
|
54
54
|
rack (>= 0.4)
|
55
|
-
rack-mount (0.8.3)
|
56
|
-
rack (>= 1.0.0)
|
57
55
|
rack-ssl (1.3.2)
|
58
56
|
rack
|
59
|
-
rack-test (0.6.
|
57
|
+
rack-test (0.6.2)
|
60
58
|
rack (>= 1.0)
|
61
|
-
rails (3.
|
62
|
-
actionmailer (= 3.
|
63
|
-
actionpack (= 3.
|
64
|
-
activerecord (= 3.
|
65
|
-
activeresource (= 3.
|
66
|
-
activesupport (= 3.
|
59
|
+
rails (3.2.9)
|
60
|
+
actionmailer (= 3.2.9)
|
61
|
+
actionpack (= 3.2.9)
|
62
|
+
activerecord (= 3.2.9)
|
63
|
+
activeresource (= 3.2.9)
|
64
|
+
activesupport (= 3.2.9)
|
67
65
|
bundler (~> 1.0)
|
68
|
-
railties (= 3.
|
69
|
-
railties (3.
|
70
|
-
actionpack (= 3.
|
71
|
-
activesupport (= 3.
|
66
|
+
railties (= 3.2.9)
|
67
|
+
railties (3.2.9)
|
68
|
+
actionpack (= 3.2.9)
|
69
|
+
activesupport (= 3.2.9)
|
72
70
|
rack-ssl (~> 1.3.2)
|
73
71
|
rake (>= 0.8.7)
|
74
72
|
rdoc (~> 3.4)
|
75
|
-
thor (
|
76
|
-
rake (0.
|
77
|
-
rdoc (3.
|
73
|
+
thor (>= 0.14.6, < 2.0)
|
74
|
+
rake (10.0.1)
|
75
|
+
rdoc (3.12)
|
78
76
|
json (~> 1.4)
|
79
77
|
rspec (2.7.0)
|
80
78
|
rspec-core (~> 2.7.0)
|
@@ -84,17 +82,18 @@ GEM
|
|
84
82
|
rspec-expectations (2.7.0)
|
85
83
|
diff-lcs (~> 1.1.2)
|
86
84
|
rspec-mocks (2.7.0)
|
87
|
-
sprockets (2.
|
85
|
+
sprockets (2.2.1)
|
88
86
|
hike (~> 1.2)
|
87
|
+
multi_json (~> 1.0)
|
89
88
|
rack (~> 1.0)
|
90
89
|
tilt (~> 1.1, != 1.3.0)
|
91
|
-
sqlite3 (1.3.
|
92
|
-
thor (0.
|
90
|
+
sqlite3 (1.3.6)
|
91
|
+
thor (0.16.0)
|
93
92
|
tilt (1.3.3)
|
94
|
-
treetop (1.4.
|
93
|
+
treetop (1.4.12)
|
95
94
|
polyglot
|
96
95
|
polyglot (>= 0.3.1)
|
97
|
-
tzinfo (0.3.
|
96
|
+
tzinfo (0.3.35)
|
98
97
|
|
99
98
|
PLATFORMS
|
100
99
|
ruby
|
@@ -102,4 +101,4 @@ PLATFORMS
|
|
102
101
|
DEPENDENCIES
|
103
102
|
acts_as_metadata!
|
104
103
|
rspec (>= 2.0.0)
|
105
|
-
sqlite3
|
104
|
+
sqlite3 (>= 1.3.5)
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# ActsAsMetadata
|
2
|
+
|
3
|
+
Dynamicly add unlimited number of indexed, searchable, validatable, strongly typed fields to your ActiveRecord models.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'acts_as_metadata'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it manually:
|
16
|
+
|
17
|
+
$ gem install acts_as_metadata
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Generate migration that creates metadata tables:
|
22
|
+
|
23
|
+
$ rails g metadata:migration
|
24
|
+
|
25
|
+
Add acts_as_metadata to your model:
|
26
|
+
|
27
|
+
class MyModel < ActiveRecord::Base
|
28
|
+
acts_as_metadata
|
29
|
+
end
|
30
|
+
|
31
|
+
and create metadata_cache column to speed up metadata extraction:
|
32
|
+
|
33
|
+
rails g migration AddMetadataCacheToMyModels metadata_cache:text
|
34
|
+
|
35
|
+
Create metadata types in your database:
|
36
|
+
|
37
|
+
mt = MetadataType.create! :tag => :sample, :name => "Sample", :datatype => :string
|
38
|
+
|
39
|
+
Its ready to use:
|
40
|
+
|
41
|
+
m = MyModel.new
|
42
|
+
m.sample = 'some string'
|
43
|
+
m.save!
|
44
|
+
|
45
|
+
Add some validations or default value if you need:
|
46
|
+
|
47
|
+
mt.default = 'some default string'
|
48
|
+
mt.mandatory = true # presence validation
|
49
|
+
mt.format = "[a-z]*" # regexp validation
|
50
|
+
mt.values = ['aaa', 'bbb', 'ccc'] # inclusion validation
|
51
|
+
mt.save!
|
52
|
+
|
53
|
+
There are more usage examples in spec directory.
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
1. Fork it
|
58
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
59
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
60
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
61
|
+
5. Create new Pull Request
|
data/acts_as_metadata.gemspec
CHANGED
@@ -21,8 +21,15 @@ module ActsAsMetadata
|
|
21
21
|
value = get_metadata(type.tag)
|
22
22
|
values = type.values.map {|v| v.is_a?(Array) ? v[1] : v } rescue []
|
23
23
|
errors.add(type.tag, I18n.t('acts_as_metadata.errors.blank')) if type.mandatory && value.blank?
|
24
|
-
|
25
|
-
|
24
|
+
if value.is_a? Array
|
25
|
+
value.each_with_index do |v, i|
|
26
|
+
errors.add("#{type.tag}_#{i}", I18n.t('acts_as_metadata.errors.format')) if values.blank? && type.format.present? && v.present? && v.to_s !~ Regexp.new("^#{type.format}$")
|
27
|
+
errors.add("#{type.tag}_#{i}", I18n.t('acts_as_metadata.errors.values')) if values.present? && v.present? && !values.include?(v)
|
28
|
+
end
|
29
|
+
else
|
30
|
+
errors.add(type.tag, I18n.t('acts_as_metadata.errors.format')) if values.blank? && type.format.present? && value.present? && value.to_s !~ Regexp.new("^#{type.format}$")
|
31
|
+
errors.add(type.tag, I18n.t('acts_as_metadata.errors.values')) if values.present? && value.present? && !values.include?(value)
|
32
|
+
end
|
26
33
|
end unless @skip_metadata_validation
|
27
34
|
end
|
28
35
|
|
@@ -120,13 +127,28 @@ module ActsAsMetadata
|
|
120
127
|
Metadata::Metadata.delete_all(:model_type => self.class.name, :model_id => self.id) unless self.id.blank?
|
121
128
|
self.metadata_types.each do |type_name|
|
122
129
|
value = self.get_metadata(type_name)
|
123
|
-
|
130
|
+
if value.is_a? Array
|
131
|
+
value.each {|v| self.metadata.build(:metadata_type => type_name, :value => v) unless v.nil? }
|
132
|
+
else
|
133
|
+
self.metadata.build(:metadata_type => type_name, :value => value) unless value.nil?
|
134
|
+
end
|
124
135
|
end
|
125
136
|
end
|
126
137
|
|
127
138
|
def load_metadata
|
128
|
-
|
129
|
-
|
139
|
+
hash = {}
|
140
|
+
metadata.each do |m|
|
141
|
+
if hash[m.metadata_type].nil?
|
142
|
+
hash[m.metadata_type] = m.value
|
143
|
+
elsif hash[m.metadata_type].is_a? Array
|
144
|
+
hash[m.metadata_type] << m.value
|
145
|
+
else
|
146
|
+
hash[m.metadata_type] = [hash[m.metadata_type], m.value]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
self.metadata_cache = {}
|
150
|
+
metadata_types.each {|type_name| set_metadata type_name, hash[type_name] }
|
151
|
+
end
|
130
152
|
end
|
131
153
|
end
|
132
154
|
end
|
data/lib/metadata/version.rb
CHANGED
@@ -36,7 +36,7 @@ describe ActsAsMetadata do
|
|
36
36
|
Metadata::Metadata.all.count.should == 0
|
37
37
|
end
|
38
38
|
|
39
|
-
it '
|
39
|
+
it 'checks presence validation' do
|
40
40
|
mt = MetadataType.first
|
41
41
|
mt.mandatory = true
|
42
42
|
mt.default = nil
|
@@ -49,10 +49,10 @@ describe ActsAsMetadata do
|
|
49
49
|
mymodel.errors.count.should == 0
|
50
50
|
end
|
51
51
|
|
52
|
-
it '
|
52
|
+
it 'checks format validation' do
|
53
53
|
mt = MetadataType.first
|
54
54
|
mt.mandatory = false
|
55
|
-
mt.format = '[a-z]'
|
55
|
+
mt.format = '[a-z]*'
|
56
56
|
mt.save!
|
57
57
|
mymodel = MyModel.new
|
58
58
|
mymodel.sample = '12323'
|
@@ -63,7 +63,7 @@ describe ActsAsMetadata do
|
|
63
63
|
mymodel.errors.count.should == 0
|
64
64
|
end
|
65
65
|
|
66
|
-
it '
|
66
|
+
it 'checks values validation' do
|
67
67
|
mt = MetadataType.first
|
68
68
|
mt.values = ['aaa', 'bbb']
|
69
69
|
mt.save!
|
@@ -75,4 +75,68 @@ describe ActsAsMetadata do
|
|
75
75
|
mymodel.save
|
76
76
|
mymodel.errors.count.should == 0
|
77
77
|
end
|
78
|
+
|
79
|
+
it 'creates multiple metadata items for array metadata' do
|
80
|
+
MetadataType.destroy_all
|
81
|
+
mt = MetadataType.default
|
82
|
+
mt.tag = :samplearray
|
83
|
+
mt.name = "Sample Array"
|
84
|
+
mt.datatype = :array
|
85
|
+
mt.save!
|
86
|
+
mymodel = MyModel.new
|
87
|
+
mymodel.samplearray = ['aaa', 'bbb', 'ccc']
|
88
|
+
mymodel.save
|
89
|
+
mymodel.errors.count.should == 0
|
90
|
+
mymodel.metadata.count.should == 3
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'loads array metadata correctly' do
|
94
|
+
mymodel = MyModel.new
|
95
|
+
mymodel.samplearray = ['aaa', 'bbb', 'ccc']
|
96
|
+
mymodel.save
|
97
|
+
mymodel.errors.count.should == 0
|
98
|
+
mymodel.metadata_cache = nil
|
99
|
+
mymodel.samplearray.count.should == 3
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'checks presence validation for array metadata' do
|
103
|
+
mt = MetadataType.first
|
104
|
+
mt.mandatory = true
|
105
|
+
mt.default = nil
|
106
|
+
mt.save!
|
107
|
+
mymodel = MyModel.new
|
108
|
+
mymodel.save
|
109
|
+
mymodel.errors.include?(:samplearray).should == true
|
110
|
+
mymodel.samplearray = ['abc']
|
111
|
+
mymodel.save
|
112
|
+
mymodel.errors.count.should == 0
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'checks format validation for array metadata' do
|
116
|
+
mt = MetadataType.first
|
117
|
+
mt.mandatory = false
|
118
|
+
mt.format = '[a-z]*'
|
119
|
+
mt.save!
|
120
|
+
mymodel = MyModel.new
|
121
|
+
mymodel.samplearray = ['123', '456', 'ccc']
|
122
|
+
mymodel.save
|
123
|
+
mymodel.errors.count.should == 2
|
124
|
+
mymodel.samplearray = ['aaa', 'bbb', 'ccc']
|
125
|
+
mymodel.save
|
126
|
+
mymodel.errors.count.should == 0
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'checks values validation for array metadata' do
|
130
|
+
mt = MetadataType.first
|
131
|
+
mt.mandatory = false
|
132
|
+
mt.values = ['aaa', 'bbb', 'ccc']
|
133
|
+
mt.save!
|
134
|
+
mymodel = MyModel.new
|
135
|
+
mymodel.samplearray = ['ddd', 'eee', 'ccc']
|
136
|
+
mymodel.save
|
137
|
+
mymodel.errors.count.should == 2
|
138
|
+
mymodel.samplearray = ['aaa', 'bbb', 'ccc']
|
139
|
+
mymodel.save
|
140
|
+
mymodel.errors.count.should == 0
|
141
|
+
end
|
78
142
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_metadata
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -50,7 +50,7 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - ! '>='
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: 1.3.5
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -58,7 +58,7 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 1.3.5
|
62
62
|
description: This gem allows to add additional database-stored fields to your models
|
63
63
|
email:
|
64
64
|
- grophen@gmail.com
|
@@ -69,8 +69,8 @@ files:
|
|
69
69
|
- .gitignore
|
70
70
|
- Gemfile
|
71
71
|
- Gemfile.lock
|
72
|
+
- README.md
|
72
73
|
- Rakefile
|
73
|
-
- Readme
|
74
74
|
- acts_as_metadata.gemspec
|
75
75
|
- lib/acts_as_metadata.rb
|
76
76
|
- lib/generators/metadata/migration/migration_generator.rb
|