has_constant 0.4.9 → 0.5.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.
- data/VERSION +1 -1
- data/has_constant.gemspec +4 -1
- data/lib/has_constant/orm/active_model.rb +65 -0
- data/lib/has_constant.rb +9 -1
- data/test/unit/orm/active_model_test.rb +69 -0
- data/test/unit/orm/active_record_test.rb +0 -1
- data/test/unit/orm/mongoid_test.rb +1 -3
- metadata +7 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.0
|
data/has_constant.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{has_constant}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.5.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["mattbeedle"]
|
@@ -27,10 +27,12 @@ Gem::Specification.new do |s|
|
|
27
27
|
"VERSION",
|
28
28
|
"has_constant.gemspec",
|
29
29
|
"lib/has_constant.rb",
|
30
|
+
"lib/has_constant/orm/active_model.rb",
|
30
31
|
"lib/has_constant/orm/active_record.rb",
|
31
32
|
"lib/has_constant/orm/mongoid.rb",
|
32
33
|
"test/has_constant_test.rb",
|
33
34
|
"test/helper.rb",
|
35
|
+
"test/unit/orm/active_model_test.rb",
|
34
36
|
"test/unit/orm/active_record_test.rb",
|
35
37
|
"test/unit/orm/mongoid_test.rb"
|
36
38
|
]
|
@@ -41,6 +43,7 @@ Gem::Specification.new do |s|
|
|
41
43
|
s.test_files = [
|
42
44
|
"test/has_constant_test.rb",
|
43
45
|
"test/helper.rb",
|
46
|
+
"test/unit/orm/active_model_test.rb",
|
44
47
|
"test/unit/orm/active_record_test.rb",
|
45
48
|
"test/unit/orm/mongoid_test.rb"
|
46
49
|
]
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module HasConstant
|
2
|
+
module Orm
|
3
|
+
module ActiveModel
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def has_constant( name, values = lambda { I18n.t(name) }, options = {} )
|
8
|
+
super(name, values, options)
|
9
|
+
|
10
|
+
singular = (options[:accessor] || name.to_s.singularize).to_s
|
11
|
+
|
12
|
+
# Add the getter method. This returns the string representation of
|
13
|
+
# the stored value
|
14
|
+
define_method(singular) do
|
15
|
+
if read_attribute(singular)
|
16
|
+
self.class.send(name)[read_attribute(singular).to_i]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
define_method("#{singular}=") do |val|
|
21
|
+
if val.instance_of?(String)
|
22
|
+
write_attribute singular.to_sym, self.class.send(name.to_s).index(val)
|
23
|
+
else
|
24
|
+
write_attribute singular.to_sym, val
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class_eval do
|
29
|
+
if respond_to?(:scope)
|
30
|
+
scope :by_constant, lambda { |constant,value| { :conditions =>
|
31
|
+
{ constant.to_sym =>
|
32
|
+
eval("#{self.to_s}.#{constant.pluralize}.index(value)") } } }
|
33
|
+
|
34
|
+
scope "#{singular}_is".to_sym, lambda { |*values| { :conditions =>
|
35
|
+
{ singular.to_sym => indexes_for(name, values) }
|
36
|
+
} }
|
37
|
+
|
38
|
+
scope "#{singular}_is_not".to_sym, lambda { |*values| {
|
39
|
+
:conditions => ["#{singular} NOT IN (?)",
|
40
|
+
indexes_for(name, values)] } }
|
41
|
+
else
|
42
|
+
named_scope :by_constant, lambda { |constant,value| {
|
43
|
+
:conditions =>
|
44
|
+
{ constant.to_sym =>
|
45
|
+
eval("#{self.to_s}.#{constant.pluralize}.index(value)") } } }
|
46
|
+
|
47
|
+
named_scope "#{singular}_is".to_sym, lambda { |*values| {
|
48
|
+
:conditions => { singular.to_sym => indexes_for(name, values) }
|
49
|
+
} }
|
50
|
+
|
51
|
+
named_scope "#{singular}_is_not".to_sym, lambda { |*values| {
|
52
|
+
:conditions => ["#{singular} NOT IN (?)",
|
53
|
+
indexes_for(name, values)] } }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
def indexes_for( name, values )
|
60
|
+
values.map { |v| self.send(name.to_sym).index(v) }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/has_constant.rb
CHANGED
@@ -1,11 +1,19 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
require 'has_constant/orm/mongoid'
|
3
|
-
require 'has_constant/orm/
|
3
|
+
require 'has_constant/orm/active_model'
|
4
4
|
require 'active_support/inflector'
|
5
5
|
|
6
6
|
module HasConstant
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
9
|
+
included do
|
10
|
+
if defined?(Mongoid) && ancestors.include?(Mongoid::Document)
|
11
|
+
send(:include, HasConstant::Orm::Mongoid)
|
12
|
+
elsif defined?(ActiveModel) && !ancestors.map(&:to_s).grep(/^ActiveModel/).blank?
|
13
|
+
send(:include, HasConstant::Orm::ActiveModel)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
9
17
|
# HasConstant takes a Proc containing an array of possible values for a field name
|
10
18
|
# The field name is inferred as the singular of the has constant name. For example
|
11
19
|
# has_constant :titles
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
setup_active_record
|
5
|
+
|
6
|
+
class User < ActiveRecord::Base
|
7
|
+
include HasConstant
|
8
|
+
end
|
9
|
+
|
10
|
+
class ActiveModelTest < Test::Unit::TestCase
|
11
|
+
context 'Instance' do
|
12
|
+
should 'save values as integers' do
|
13
|
+
User.has_constant :salutations, %w(Mr Mrs)
|
14
|
+
u = User.new(:salutation => 'Mr')
|
15
|
+
u.save!
|
16
|
+
assert_equal 'Mr', u.salutation
|
17
|
+
assert_equal 0, u.attributes['salutation']
|
18
|
+
end
|
19
|
+
|
20
|
+
should 'default values to translated values list' do
|
21
|
+
I18n.stubs(:t).returns(['a', 'b'])
|
22
|
+
User.has_constant :salutations
|
23
|
+
assert_equal ['a', 'b'], User.salutations
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'accessor' do
|
27
|
+
setup do
|
28
|
+
User.has_constant :titles, ['Mr', 'Mrs'], :accessor => :salutation
|
29
|
+
@user = User.new(:salutation => 'Mr')
|
30
|
+
end
|
31
|
+
|
32
|
+
should 'work store the values in the correct field ' do
|
33
|
+
@user.save!
|
34
|
+
assert_equal 0, @user.attributes['salutation']
|
35
|
+
end
|
36
|
+
|
37
|
+
should 'use the accessor to get values' do
|
38
|
+
assert_equal 'Mr', @user.salutation
|
39
|
+
end
|
40
|
+
|
41
|
+
should 'use constant name for values list' do
|
42
|
+
assert_equal %w(Mr Mrs), User.titles
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'scopes' do
|
48
|
+
setup do
|
49
|
+
User.has_constant :salutations, %w(Mr Mrs)
|
50
|
+
@man = User.create!(:salutation => 'Mr')
|
51
|
+
@woman = User.create!(:salutation => 'Mrs')
|
52
|
+
end
|
53
|
+
|
54
|
+
should 'provide by_constant scope' do
|
55
|
+
assert_equal 1, User.by_constant('salutation', 'Mr').count
|
56
|
+
assert_equal @man, User.by_constant('salutation', 'Mr').first
|
57
|
+
end
|
58
|
+
|
59
|
+
should 'provide singular_is scope' do
|
60
|
+
assert_equal 1, User.salutation_is('Mr').count
|
61
|
+
assert_equal @man, User.salutation_is('Mr').first
|
62
|
+
end
|
63
|
+
|
64
|
+
should 'provide singular_is_not scope' do
|
65
|
+
assert_equal 1, User.salutation_is_not('Mr').count
|
66
|
+
assert_equal @woman, User.salutation_is_not('Mr').first
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end if defined?(ActiveRecord)
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'helper'
|
2
|
+
require 'mocha'
|
2
3
|
|
3
4
|
setup_mongoid
|
4
5
|
|
5
6
|
class MongoUser
|
6
7
|
include Mongoid::Document
|
7
8
|
include HasConstant
|
8
|
-
include HasConstant::Orm::Mongoid
|
9
9
|
|
10
10
|
has_constant :salutations, ['Mr', 'Mrs']
|
11
11
|
end if defined?(Mongoid)
|
@@ -13,7 +13,6 @@ end if defined?(Mongoid)
|
|
13
13
|
class MongoUserWithProc
|
14
14
|
include Mongoid::Document
|
15
15
|
include HasConstant
|
16
|
-
include HasConstant::Orm::Mongoid
|
17
16
|
|
18
17
|
has_constant :salutations, lambda { ['Mr', 'Mrs'] }
|
19
18
|
end if defined?(Mongoid)
|
@@ -21,7 +20,6 @@ end if defined?(Mongoid)
|
|
21
20
|
class MongoUserWithout
|
22
21
|
include Mongoid::Document
|
23
22
|
include HasConstant
|
24
|
-
include HasConstant::Orm::Mongoid
|
25
23
|
end
|
26
24
|
|
27
25
|
class MongoidTest < Test::Unit::TestCase
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_constant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 5
|
9
|
+
- 0
|
10
|
+
version: 0.5.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- mattbeedle
|
@@ -80,10 +80,12 @@ files:
|
|
80
80
|
- VERSION
|
81
81
|
- has_constant.gemspec
|
82
82
|
- lib/has_constant.rb
|
83
|
+
- lib/has_constant/orm/active_model.rb
|
83
84
|
- lib/has_constant/orm/active_record.rb
|
84
85
|
- lib/has_constant/orm/mongoid.rb
|
85
86
|
- test/has_constant_test.rb
|
86
87
|
- test/helper.rb
|
88
|
+
- test/unit/orm/active_model_test.rb
|
87
89
|
- test/unit/orm/active_record_test.rb
|
88
90
|
- test/unit/orm/mongoid_test.rb
|
89
91
|
has_rdoc: true
|
@@ -123,5 +125,6 @@ summary: Allows certain fields to be limited to a set of values
|
|
123
125
|
test_files:
|
124
126
|
- test/has_constant_test.rb
|
125
127
|
- test/helper.rb
|
128
|
+
- test/unit/orm/active_model_test.rb
|
126
129
|
- test/unit/orm/active_record_test.rb
|
127
130
|
- test/unit/orm/mongoid_test.rb
|