attribute_enums 0.1.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/.gitignore +1 -3
  2. data/.rspec +1 -0
  3. data/CHANGELOG +4 -0
  4. data/Gemfile +1 -1
  5. data/README.markdown +61 -59
  6. data/attribute_enums.gemspec +5 -16
  7. data/lib/attribute_enums.rb +0 -4
  8. data/lib/attribute_enums/active_record.rb +122 -0
  9. data/lib/attribute_enums/common.rb +168 -0
  10. data/lib/attribute_enums/railtie.rb +2 -2
  11. data/lib/attribute_enums/version.rb +1 -1
  12. data/spec/common_and_active_record/ask_methods_options_spec.rb +58 -0
  13. data/spec/common_and_active_record/attribute_name_spec.rb +7 -0
  14. data/spec/common_and_active_record/boolean_options_spec.rb +43 -0
  15. data/spec/common_and_active_record/default_options_spec.rb +77 -0
  16. data/spec/common_and_active_record/i18n_options_spec.rb +116 -0
  17. data/spec/common_and_active_record/in_options_spec.rb +38 -0
  18. data/spec/common_and_active_record/readme_user_example_spec.rb +87 -0
  19. data/spec/common_and_active_record/scopeds_options_spec.rb +114 -0
  20. data/spec/common_and_active_record/validate_options_spec.rb +82 -0
  21. data/spec/spec_helper.rb +5 -12
  22. data/spec/support/locales/person_en.yml +14 -0
  23. data/spec/support/locales/person_zh-CN.yml +14 -0
  24. data/spec/support/locales/user_en.yml +7 -15
  25. data/spec/support/models/person.rb +23 -0
  26. metadata +34 -229
  27. data/Guardfile +0 -22
  28. data/lib/attribute_enums/active_record_extension.rb +0 -88
  29. data/spec/models/active_record/user_spec.rb +0 -67
  30. data/spec/rails3_0_app/.gitignore +0 -4
  31. data/spec/rails3_0_app/.rspec +0 -1
  32. data/spec/rails3_0_app/Gemfile +0 -13
  33. data/spec/rails3_0_app/Rakefile +0 -7
  34. data/spec/rails3_0_app/app/controllers/application_controller.rb +0 -3
  35. data/spec/rails3_0_app/app/helpers/application_helper.rb +0 -2
  36. data/spec/rails3_0_app/app/models/user.rb +0 -2
  37. data/spec/rails3_0_app/app/views/layouts/application.html.erb +0 -14
  38. data/spec/rails3_0_app/config.ru +0 -4
  39. data/spec/rails3_0_app/config/application.rb +0 -44
  40. data/spec/rails3_0_app/config/boot.rb +0 -6
  41. data/spec/rails3_0_app/config/database.yml +0 -19
  42. data/spec/rails3_0_app/config/environment.rb +0 -6
  43. data/spec/rails3_0_app/config/environments/development.rb +0 -26
  44. data/spec/rails3_0_app/config/environments/production.rb +0 -49
  45. data/spec/rails3_0_app/config/environments/test.rb +0 -35
  46. data/spec/rails3_0_app/config/initializers/backtrace_silencers.rb +0 -7
  47. data/spec/rails3_0_app/config/initializers/inflections.rb +0 -10
  48. data/spec/rails3_0_app/config/initializers/mime_types.rb +0 -5
  49. data/spec/rails3_0_app/config/initializers/secret_token.rb +0 -7
  50. data/spec/rails3_0_app/config/initializers/session_store.rb +0 -8
  51. data/spec/rails3_0_app/config/locales/en.yml +0 -5
  52. data/spec/rails3_0_app/config/routes.rb +0 -58
  53. data/spec/rails3_0_app/db/migrate/20111129011508_create_users.rb +0 -14
  54. data/spec/rails3_0_app/db/schema.rb +0 -23
  55. data/spec/rails3_0_app/db/seeds.rb +0 -7
  56. data/spec/rails3_0_app/lib/tasks/.gitkeep +0 -0
  57. data/spec/rails3_0_app/public/404.html +0 -26
  58. data/spec/rails3_0_app/public/422.html +0 -26
  59. data/spec/rails3_0_app/public/500.html +0 -26
  60. data/spec/rails3_0_app/public/favicon.ico +0 -0
  61. data/spec/rails3_0_app/public/images/rails.png +0 -0
  62. data/spec/rails3_0_app/public/index.html +0 -239
  63. data/spec/rails3_0_app/public/robots.txt +0 -5
  64. data/spec/rails3_0_app/public/stylesheets/.gitkeep +0 -0
  65. data/spec/rails3_0_app/script/rails +0 -6
  66. data/spec/rails3_0_app/vendor/plugins/.gitkeep +0 -0
@@ -1,10 +1,10 @@
1
- require 'attribute_enums/active_record_extension'
1
+ require 'attribute_enums/active_record'
2
2
 
3
3
  module AttributeEnums
4
4
  class Railtie < ::Rails::Railtie
5
5
  initializer 'attribute_enums' do |app|
6
6
  ActiveSupport.on_load :active_record do
7
- include AttributeEnums::ActiveRecordExtension
7
+ include ::AttributeEnums::ActiveRecord
8
8
  end
9
9
  end
10
10
  end
@@ -1,4 +1,4 @@
1
1
  module AttributeEnums
2
- VERSION = "0.1.8"
2
+ VERSION = "0.2.0"
3
3
  end
4
4
 
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe ":methods options" do
4
+
5
+ describe "string attribute" do
6
+ describe "default methods" do
7
+ let(:model_klass) do
8
+ _person = Person.dup
9
+ _person.instance_eval do
10
+ include AttributeEnums::ActiveRecord
11
+ attribute_enums :gender, in: [:male, :female], ask_methods: true
12
+ end
13
+ _person
14
+ end
15
+
16
+ subject { model_klass.new }
17
+
18
+ it do
19
+ subject.should respond_to(:male?)
20
+ subject.should respond_to(:female?)
21
+ end
22
+ it do
23
+ subject.gender = :male
24
+ subject.male?.should be_true
25
+ subject.female?.should be_false
26
+ end
27
+ end
28
+
29
+
30
+ describe "prefix methods" do
31
+ let(:model_klass) do
32
+ _person = Person.dup
33
+ _person.instance_eval do
34
+ include AttributeEnums::ActiveRecord
35
+ attribute_enums :gender, in: [:male, :female], ask_methods: {prefix: 'gender_'}
36
+ end
37
+ _person
38
+ end
39
+
40
+ subject { model_klass.new }
41
+
42
+ it do
43
+ subject.should respond_to(:gender_male?)
44
+ end
45
+ it do
46
+ subject.gender = :male
47
+ subject.gender_male?.should be_true
48
+ end
49
+ end
50
+ end
51
+
52
+
53
+ describe "boolean attribute" do
54
+ # this option is invalid boolean attribute.
55
+ end
56
+
57
+
58
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe "attribute_name options" do
4
+
5
+ pending
6
+
7
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe ":boolean options" do
4
+
5
+ let(:model_klass) do
6
+ _person = Person.dup
7
+ _person.instance_eval do
8
+ include AttributeEnums::ActiveRecord
9
+ attribute_enums :enable, boolean: true
10
+ end
11
+ _person
12
+ end
13
+
14
+ subject { model_klass.new }
15
+
16
+ it do
17
+ model_klass.should respond_to(:get_enable_values)
18
+ end
19
+
20
+ it do
21
+ model_klass.get_enable_values.should == [['Yes', true], ['No', false]]
22
+ end
23
+
24
+ it do
25
+ subject.should respond_to(:enable_text)
26
+ end
27
+
28
+ it do
29
+ subject.enable = nil
30
+ subject.enable_text.should == ''
31
+ end
32
+
33
+ it do
34
+ subject.enable = true
35
+ subject.enable_text.should == 'Yes'
36
+ end
37
+
38
+ it do
39
+ subject.enable = false
40
+ subject.enable_text.should == 'No'
41
+ end
42
+
43
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe ":default options" do
4
+
5
+ describe "string attribute" do
6
+
7
+ let(:model_klass) do
8
+ _person = Person.dup
9
+ _person.instance_eval do
10
+ include AttributeEnums::ActiveRecord
11
+ attribute_enums :gender, in: [:male, :female], default: :male
12
+ end
13
+ _person
14
+ end
15
+
16
+ subject { model_klass.new }
17
+
18
+ it do
19
+ subject.gender = nil
20
+ subject.valid?
21
+ subject.gender.should == :male
22
+ end
23
+
24
+ it do
25
+ subject.gender = :female
26
+ subject.valid?
27
+ subject.gender.should == :female
28
+ end
29
+
30
+ it do
31
+ my_person = Person.dup
32
+ lambda do
33
+ my_person.instance_eval do
34
+ include AttributeEnums::ActiveRecord
35
+ attribute_enums :gender, in: [:male, :female], default: :unknow
36
+ end
37
+ end.should raise_error(Exception, /default value not match/)
38
+ end
39
+
40
+ it do
41
+ my_person = Person.dup
42
+ lambda do
43
+ my_person.instance_eval do
44
+ include AttributeEnums::ActiveRecord
45
+ attribute_enums :gender, in: [:male, :female], default: 'male'
46
+ end
47
+ end.should_not raise_error(Exception, /default value not match/)
48
+ end
49
+ end
50
+
51
+ describe "boolean attribute" do
52
+
53
+ let(:model_klass) do
54
+ _person = Person.dup
55
+ _person.instance_eval do
56
+ include AttributeEnums::ActiveRecord
57
+ attribute_enums :enable, boolean: true, default: true
58
+ end
59
+ _person
60
+ end
61
+
62
+ subject { model_klass.new }
63
+
64
+ it do
65
+ subject.enable = nil
66
+ subject.valid?
67
+ subject.enable.should == true
68
+ end
69
+
70
+ it do
71
+ subject.enable = false
72
+ subject.valid?
73
+ subject.enable.should == false
74
+ end
75
+ end
76
+
77
+ end
@@ -0,0 +1,116 @@
1
+ #coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe ":i18n options" do
5
+ #
6
+ # it i18n xxx_text and get_xxx_values methods result
7
+ #
8
+ describe "string attribute" do
9
+ describe "default translation namespace" do
10
+ let(:model_klass) do
11
+ _person = Person.dup
12
+ _person.instance_eval do
13
+ include AttributeEnums::ActiveRecord
14
+ attribute_enums :gender, in: [:male, :female], i18n: true
15
+ end
16
+ _person
17
+ end
18
+
19
+ subject { model_klass.new }
20
+
21
+ it do
22
+ model_klass.should respond_to(:get_gender_values)
23
+ end
24
+
25
+ it do
26
+ I18n.locale = :en
27
+ model_klass.get_gender_values.should == [['Boy', 'male'], ['Girl', 'female']]
28
+ end
29
+
30
+ it do
31
+ I18n.locale = :'zh-TW'
32
+ model_klass.get_gender_values.first[0].should start_with('translation missing:')
33
+ end
34
+
35
+ it do
36
+ subject.should respond_to(:gender_text)
37
+ end
38
+
39
+ it do
40
+ I18n.locale = :en
41
+ subject.gender = :male
42
+ subject.gender_text.should == 'Boy'
43
+ end
44
+
45
+ it do
46
+ I18n.locale = :'zh-CN'
47
+ subject.gender = :male
48
+ subject.gender_text.should == '男'
49
+ end
50
+
51
+ it do
52
+ I18n.locale = :'zh-TW'
53
+ subject.gender = :male
54
+ subject.gender_text.should start_with('translation missing:')
55
+ end
56
+ end
57
+
58
+ describe "prefix translation namespace" do
59
+ let(:model_klass) do
60
+ _person = Person.dup
61
+ _person.instance_eval do
62
+ include AttributeEnums::ActiveRecord
63
+ attribute_enums :gender, in: [:male, :female], i18n: {t_prefix: :person_}
64
+ end
65
+ _person
66
+ end
67
+
68
+ subject { model_klass.new }
69
+
70
+ it do
71
+ I18n.locale = :en
72
+ model_klass.get_gender_values.first[0].should_not start_with('translation missing:')
73
+ model_klass.get_gender_values.should == [['Boy', 'male'], ['Girl', 'female']]
74
+ end
75
+
76
+ it do
77
+ I18n.locale = :en
78
+ subject.gender = :male
79
+ subject.gender_text.should == 'Boy'
80
+ end
81
+ end
82
+ end
83
+
84
+
85
+
86
+
87
+ describe "boolean attribute" do
88
+ describe "default namespace" do
89
+ let(:model_klass) do
90
+ _person = Person.dup
91
+ _person.instance_eval do
92
+ include AttributeEnums::ActiveRecord
93
+ attribute_enums :enable, boolean: true, i18n: true
94
+ end
95
+ _person
96
+ end
97
+
98
+ subject { model_klass.new }
99
+
100
+ it do
101
+ I18n.locale = :en
102
+ model_klass.get_enable_values.should == [['Enable', true], ['Disable', false]]
103
+ end
104
+
105
+ it do
106
+ subject.should respond_to(:enable_text)
107
+ end
108
+
109
+ it do
110
+ I18n.locale = :en
111
+ subject.enable = true
112
+ subject.enable_text.should == 'Enable'
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe ":in options" do
4
+
5
+ let(:model_klass) do
6
+ _person = Person.dup
7
+ _person.instance_eval do
8
+ include AttributeEnums::ActiveRecord
9
+ attribute_enums :gender, in: [:male, :female]
10
+ end
11
+ _person
12
+ end
13
+
14
+ subject { model_klass.new }
15
+
16
+ it do
17
+ model_klass.should respond_to(:get_gender_values)
18
+ end
19
+
20
+ it do
21
+ model_klass.get_gender_values.should == [['male', 'male'], ['female', 'female']]
22
+ end
23
+
24
+ it do
25
+ subject.should respond_to(:gender_text)
26
+ end
27
+
28
+ it do
29
+ subject.gender = nil
30
+ subject.gender_text.should == ''
31
+ end
32
+
33
+ it do
34
+ subject.gender = :male
35
+ subject.gender_text.should == 'male'
36
+ end
37
+
38
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe "readme user example" do
4
+
5
+ before :all do
6
+ User.instance_eval do
7
+ include AttributeEnums::ActiveRecord
8
+ attribute_enums :status, in: [:pending, :accredited], default: :pending, scopeds: true, ask_methods: true, i18n: true, validate: true
9
+ attribute_enums :gender, in: [:female, :male], default: :male, scopeds: true, ask_methods: true, i18n: true, validate: true
10
+
11
+ attribute_enums :enable, boolean: true, default: true, scopeds: :disable, i18n: {t_prefix: :person_}
12
+ attribute_enums :removed, boolean: true, default: true, scopeds: :existed, i18n: true
13
+ end
14
+ end
15
+
16
+ subject { User.new }
17
+
18
+ =begin
19
+ User.get_gender_values
20
+ User.get_enable_values
21
+ user = User.new(gender: :male, enable: true)
22
+ user.gender_text
23
+ user.enable_text
24
+ =end
25
+
26
+ =begin
27
+ User.get_gender_values #=> [["Girl", "female"], ["Boy", "male"]]
28
+ User.get_enable_values #=> [["Enable", true], ["Disable", false]]
29
+ user = User.new(gender: :male, enable: true)
30
+ user.gender_text #=> "Boy"
31
+ user.enable_text #=> "Enable"
32
+ =end
33
+
34
+ it do
35
+ User.get_gender_values.should == [["Girl", "female"], ["Boy", "male"]]
36
+ User.get_enable_values.should == [["Enable", true], ["Disable", false]]
37
+ user = User.new(gender: :male, enable: true)
38
+ user.gender_text.should == "Boy"
39
+ user.enable_text.should == "Enable"
40
+ end
41
+
42
+ =begin
43
+ User.male #=> User.where(gender: :male)
44
+ User.female #=> User.where(gender: :female)
45
+ User.enable #=> User.where(enable: true)
46
+ User.disable #=> User.where(enable: false)
47
+ =end
48
+
49
+ it do
50
+ User.delete_all
51
+ user_a = User.create!(gender: :male)
52
+ user_b = User.create!(gender: :female)
53
+ User.male.map(&:id).should == [user_a.id]
54
+ User.female.map(&:id).should == [user_b.id]
55
+
56
+ User.delete_all
57
+ user_c = User.create!(enable: true)
58
+ user_d = User.create!(enable: false)
59
+ User.enable.map(&:id).should == [user_c.id]
60
+ User.disable.map(&:id).should == [user_d.id]
61
+ end
62
+
63
+ =begin
64
+ user = User.new(gender: :male, enable: true)
65
+ user.male? #=> true
66
+ user.female? #=> false
67
+ =end
68
+
69
+ it do
70
+ user = User.new(gender: :male, enable: true)
71
+ user.male?.should == true
72
+ user.female?.should == false
73
+ end
74
+
75
+ =begin
76
+ user = User.new
77
+ user.valid?
78
+ user.gender #=> :male
79
+ =end
80
+
81
+ it do
82
+ user = User.new(gender: :male, enable: true)
83
+ user.valid?
84
+ user.gender.should == :male
85
+ end
86
+
87
+ end