acts_as_metadata 0.3.8 → 0.3.9

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.
data/Gemfile.lock CHANGED
@@ -1,37 +1,37 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- acts_as_metadata (0.3.5)
4
+ acts_as_metadata (0.3.8)
5
5
  rails (>= 3.0.0)
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
- actionmailer (3.2.9)
11
- actionpack (= 3.2.9)
10
+ actionmailer (3.2.12)
11
+ actionpack (= 3.2.12)
12
12
  mail (~> 2.4.4)
13
- actionpack (3.2.9)
14
- activemodel (= 3.2.9)
15
- activesupport (= 3.2.9)
13
+ actionpack (3.2.12)
14
+ activemodel (= 3.2.12)
15
+ activesupport (= 3.2.12)
16
16
  builder (~> 3.0.0)
17
17
  erubis (~> 2.7.0)
18
18
  journey (~> 1.0.4)
19
- rack (~> 1.4.0)
19
+ rack (~> 1.4.5)
20
20
  rack-cache (~> 1.2)
21
21
  rack-test (~> 0.6.1)
22
22
  sprockets (~> 2.2.1)
23
- activemodel (3.2.9)
24
- activesupport (= 3.2.9)
23
+ activemodel (3.2.12)
24
+ activesupport (= 3.2.12)
25
25
  builder (~> 3.0.0)
26
- activerecord (3.2.9)
27
- activemodel (= 3.2.9)
28
- activesupport (= 3.2.9)
26
+ activerecord (3.2.12)
27
+ activemodel (= 3.2.12)
28
+ activesupport (= 3.2.12)
29
29
  arel (~> 3.0.2)
30
30
  tzinfo (~> 0.3.29)
31
- activeresource (3.2.9)
32
- activemodel (= 3.2.9)
33
- activesupport (= 3.2.9)
34
- activesupport (3.2.9)
31
+ activeresource (3.2.12)
32
+ activemodel (= 3.2.12)
33
+ activesupport (= 3.2.12)
34
+ activesupport (3.2.12)
35
35
  i18n (~> 0.6)
36
36
  multi_json (~> 1.0)
37
37
  arel (3.0.2)
@@ -39,40 +39,40 @@ GEM
39
39
  diff-lcs (1.1.3)
40
40
  erubis (2.7.0)
41
41
  hike (1.2.1)
42
- i18n (0.6.1)
42
+ i18n (0.6.4)
43
43
  journey (1.0.4)
44
- json (1.7.5)
44
+ json (1.7.7)
45
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.19)
50
- multi_json (1.3.7)
49
+ mime-types (1.21)
50
+ multi_json (1.6.1)
51
51
  polyglot (0.3.3)
52
- rack (1.4.1)
52
+ rack (1.4.5)
53
53
  rack-cache (1.2)
54
54
  rack (>= 0.4)
55
- rack-ssl (1.3.2)
55
+ rack-ssl (1.3.3)
56
56
  rack
57
57
  rack-test (0.6.2)
58
58
  rack (>= 1.0)
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)
59
+ rails (3.2.12)
60
+ actionmailer (= 3.2.12)
61
+ actionpack (= 3.2.12)
62
+ activerecord (= 3.2.12)
63
+ activeresource (= 3.2.12)
64
+ activesupport (= 3.2.12)
65
65
  bundler (~> 1.0)
66
- railties (= 3.2.9)
67
- railties (3.2.9)
68
- actionpack (= 3.2.9)
69
- activesupport (= 3.2.9)
66
+ railties (= 3.2.12)
67
+ railties (3.2.12)
68
+ actionpack (= 3.2.12)
69
+ activesupport (= 3.2.12)
70
70
  rack-ssl (~> 1.3.2)
71
71
  rake (>= 0.8.7)
72
72
  rdoc (~> 3.4)
73
73
  thor (>= 0.14.6, < 2.0)
74
- rake (10.0.2)
75
- rdoc (3.12)
74
+ rake (10.0.3)
75
+ rdoc (3.12.2)
76
76
  json (~> 1.4)
77
77
  rspec (2.7.0)
78
78
  rspec-core (~> 2.7.0)
@@ -82,18 +82,18 @@ GEM
82
82
  rspec-expectations (2.7.0)
83
83
  diff-lcs (~> 1.1.2)
84
84
  rspec-mocks (2.7.0)
85
- sprockets (2.2.1)
85
+ sprockets (2.2.2)
86
86
  hike (~> 1.2)
87
87
  multi_json (~> 1.0)
88
88
  rack (~> 1.0)
89
89
  tilt (~> 1.1, != 1.3.0)
90
90
  sqlite3 (1.3.6)
91
- thor (0.16.0)
92
- tilt (1.3.3)
91
+ thor (0.17.0)
92
+ tilt (1.3.5)
93
93
  treetop (1.4.12)
94
94
  polyglot
95
95
  polyglot (>= 0.3.1)
96
- tzinfo (0.3.35)
96
+ tzinfo (0.3.37)
97
97
 
98
98
  PLATFORMS
99
99
  ruby
@@ -19,16 +19,17 @@ module ActsAsMetadata
19
19
  metadata_types.each do |type_name|
20
20
  type = MetadataType.type(type_name, metadata_scope)
21
21
  value = get_metadata(type.tag)
22
- values = type.values.map {|v| v.is_a?(Array) ? v[1] : v } rescue []
23
- errors.add(type.tag, I18n.t('acts_as_metadata.errors.blank')) if type.mandatory && value.blank?
22
+ values = type.values.map {|v| v.is_a?(Array) ? v[1].to_s : v.to_s } rescue []
24
23
  if value.is_a? Array
24
+ errors.add(type.tag, I18n.t('acts_as_metadata.errors.blank')) if type.mandatory && (value.blank? || value.map(&:blank?).reduce(:&))
25
25
  value.each_with_index do |v, i|
26
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
27
  errors.add("#{type.tag}_#{i}", I18n.t('acts_as_metadata.errors.values')) if values.present? && v.present? && !values.include?(v)
28
28
  end
29
29
  else
30
+ errors.add(type.tag, I18n.t('acts_as_metadata.errors.blank')) if type.mandatory && value.blank?
30
31
  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
+ errors.add(type.tag, I18n.t('acts_as_metadata.errors.values')) if values.present? && value.present? && !values.include?(value.to_s)
32
33
  end
33
34
  end unless @skip_metadata_validation
34
35
  end
@@ -113,7 +114,7 @@ module ActsAsMetadata
113
114
 
114
115
  def get_metadata(name)
115
116
  load_metadata unless metadata_cache.is_a?(Hash)
116
- metadata_cache[name]
117
+ metadata_cache[name].nil? ? MetadataType.type(name, metadata_scope).default : metadata_cache[name]
117
118
  end
118
119
 
119
120
  def set_metadata(name, value)
@@ -121,6 +122,8 @@ module ActsAsMetadata
121
122
  raise NoMethodError if type.nil?
122
123
  load_metadata unless metadata_cache.is_a?(Hash)
123
124
  self.metadata_cache[name] = type.type_cast(value) || type.type_cast(type.default)
125
+ self.metadata_cache[name] = [self.metadata_cache[name]].compact if type.multiple && !self.metadata_cache[name].is_a?(Array)
126
+ self.metadata_cache[name] = self.metadata_cache[name].first if !type.multiple && self.metadata_cache[name].is_a?(Array)
124
127
  end
125
128
 
126
129
  def save_metadata
@@ -40,8 +40,10 @@ class MetadataType < ActiveRecord::Base
40
40
  end
41
41
 
42
42
  def type_cast(value)
43
+ return value.map {|x| type_cast x } if value.is_a? Array
43
44
  return nil if value.nil? && datatype != 'boolean'
44
- return value unless value.is_a? String || value.nil?
45
+ return value unless value.is_a?(String) || value.nil?
46
+
45
47
  case datatype
46
48
  when 'date'
47
49
  ActiveRecord::ConnectionAdapters::Column.string_to_date value
@@ -78,6 +80,10 @@ class MetadataType < ActiveRecord::Base
78
80
  values.each {|v| v.is_a?(Array) ? v.each(&:strip!) : v.strip! } if values.is_a? Array
79
81
  end
80
82
 
83
+ def default
84
+ type_cast(attributes['default'])
85
+ end
86
+
81
87
  def default_json
82
88
  self.default.to_json
83
89
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsMetadata
2
- VERSION = "0.3.8"
2
+ VERSION = "0.3.9"
3
3
  end
@@ -90,6 +90,21 @@ describe ActsAsMetadata do
90
90
  mymodel.metadata.count.should == 3
91
91
  end
92
92
 
93
+ it 'always returns array for multiple type' do
94
+ MetadataType.destroy_all
95
+ mt = MetadataType.default
96
+ mt.tag = :samplearray
97
+ mt.name = "Sample Array"
98
+ mt.multiple = true
99
+ mt.save!
100
+ mymodel = MyModel.new
101
+ mymodel.samplearray = 'aaa'
102
+ mymodel.save
103
+ mymodel.errors.count.should == 0
104
+ mymodel.metadata.count.should == 1
105
+ mymodel.samplearray.should == ['aaa']
106
+ end
107
+
93
108
  it 'loads multiple metadata correctly' do
94
109
  mymodel = MyModel.new
95
110
  mymodel.samplearray = ['aaa', 'bbb', 'ccc']
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.3.8
4
+ version: 0.3.9
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-19 00:00:00.000000000 Z
12
+ date: 2013-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails