edave-enumerated_attribute 0.2.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. data/.gitignore +15 -0
  2. data/CHANGELOG.rdoc +49 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +558 -0
  5. data/Rakefile +30 -0
  6. data/init.rb +1 -0
  7. data/lib/enumerated_attribute.rb +24 -0
  8. data/lib/enumerated_attribute/attribute.rb +79 -0
  9. data/lib/enumerated_attribute/attribute/arguments.rb +48 -0
  10. data/lib/enumerated_attribute/attribute/attribute_descriptor.rb +51 -0
  11. data/lib/enumerated_attribute/attribute/class_methods.rb +45 -0
  12. data/lib/enumerated_attribute/attribute/instance_methods.rb +96 -0
  13. data/lib/enumerated_attribute/integrations.rb +33 -0
  14. data/lib/enumerated_attribute/integrations/active_record.rb +114 -0
  15. data/lib/enumerated_attribute/integrations/datamapper.rb +6 -0
  16. data/lib/enumerated_attribute/integrations/default.rb +25 -0
  17. data/lib/enumerated_attribute/integrations/object.rb +47 -0
  18. data/lib/enumerated_attribute/method_definition_dsl.rb +142 -0
  19. data/lib/enumerated_attribute/rails_helpers.rb +99 -0
  20. data/spec/active_record/active_record_spec.rb +384 -0
  21. data/spec/active_record/association_test_classes.rb +40 -0
  22. data/spec/active_record/associations_spec.rb +130 -0
  23. data/spec/active_record/cfg.rb +6 -0
  24. data/spec/active_record/df.rb +12 -0
  25. data/spec/active_record/inheritance_classes.rb +19 -0
  26. data/spec/active_record/inheritance_spec.rb +60 -0
  27. data/spec/active_record/race_car.rb +15 -0
  28. data/spec/active_record/single_table_inheritance_spec.rb +0 -0
  29. data/spec/active_record/sti_classes.rb +10 -0
  30. data/spec/active_record/sti_spec.rb +41 -0
  31. data/spec/active_record/test_in_memory.rb +71 -0
  32. data/spec/car.rb +67 -0
  33. data/spec/cfg.rb +8 -0
  34. data/spec/inheritance_classes.rb +16 -0
  35. data/spec/inheritance_spec.rb +80 -0
  36. data/spec/new_and_method_missing_spec.rb +73 -0
  37. data/spec/plural.rb +8 -0
  38. data/spec/poro_spec.rb +420 -0
  39. data/spec/rails/README +243 -0
  40. data/spec/rails/Rakefile +10 -0
  41. data/spec/rails/app/controllers/application_controller.rb +10 -0
  42. data/spec/rails/app/controllers/form_test_controller.rb +38 -0
  43. data/spec/rails/app/helpers/application_helper.rb +3 -0
  44. data/spec/rails/app/helpers/form_test_helper.rb +2 -0
  45. data/spec/rails/app/models/user.rb +9 -0
  46. data/spec/rails/app/views/form_test/form.html.erb +1 -0
  47. data/spec/rails/app/views/form_test/form_for.html.erb +10 -0
  48. data/spec/rails/app/views/form_test/form_tag.html.erb +9 -0
  49. data/spec/rails/app/views/form_test/index.html.erb +6 -0
  50. data/spec/rails/app/views/layouts/application.html.erb +11 -0
  51. data/spec/rails/config/boot.rb +110 -0
  52. data/spec/rails/config/database.yml +22 -0
  53. data/spec/rails/config/environment.rb +45 -0
  54. data/spec/rails/config/environments/development.rb +17 -0
  55. data/spec/rails/config/environments/production.rb +28 -0
  56. data/spec/rails/config/environments/test.rb +28 -0
  57. data/spec/rails/config/initializers/backtrace_silencers.rb +7 -0
  58. data/spec/rails/config/initializers/inflections.rb +10 -0
  59. data/spec/rails/config/initializers/mime_types.rb +5 -0
  60. data/spec/rails/config/initializers/new_rails_defaults.rb +19 -0
  61. data/spec/rails/config/initializers/session_store.rb +15 -0
  62. data/spec/rails/config/locales/en.yml +5 -0
  63. data/spec/rails/config/routes.rb +43 -0
  64. data/spec/rails/db/development.sqlite3 +0 -0
  65. data/spec/rails/db/migrate/20090804230221_create_sessions.rb +16 -0
  66. data/spec/rails/db/migrate/20090804230546_create_users.rb +21 -0
  67. data/spec/rails/db/schema.rb +35 -0
  68. data/spec/rails/db/test.sqlite3 +0 -0
  69. data/spec/rails/public/404.html +30 -0
  70. data/spec/rails/public/422.html +30 -0
  71. data/spec/rails/public/500.html +30 -0
  72. data/spec/rails/public/favicon.ico +0 -0
  73. data/spec/rails/public/images/rails.png +0 -0
  74. data/spec/rails/public/index.html +275 -0
  75. data/spec/rails/public/javascripts/application.js +2 -0
  76. data/spec/rails/public/javascripts/controls.js +963 -0
  77. data/spec/rails/public/javascripts/dragdrop.js +973 -0
  78. data/spec/rails/public/javascripts/effects.js +1128 -0
  79. data/spec/rails/public/javascripts/prototype.js +4320 -0
  80. data/spec/rails/public/robots.txt +5 -0
  81. data/spec/rails/public/stylesheets/scaffold.css +54 -0
  82. data/spec/rails/script/about +4 -0
  83. data/spec/rails/script/autospec +6 -0
  84. data/spec/rails/script/console +3 -0
  85. data/spec/rails/script/dbconsole +3 -0
  86. data/spec/rails/script/destroy +3 -0
  87. data/spec/rails/script/generate +3 -0
  88. data/spec/rails/script/performance/benchmarker +3 -0
  89. data/spec/rails/script/performance/profiler +3 -0
  90. data/spec/rails/script/plugin +3 -0
  91. data/spec/rails/script/runner +3 -0
  92. data/spec/rails/script/server +3 -0
  93. data/spec/rails/script/spec +10 -0
  94. data/spec/rails/script/spec_server +9 -0
  95. data/spec/rails/spec/controllers/form_test_controller_spec.rb +41 -0
  96. data/spec/rails/spec/integrations/enum_select_spec.rb +75 -0
  97. data/spec/rails/spec/matchers.rb +12 -0
  98. data/spec/rails/spec/rcov.opts +2 -0
  99. data/spec/rails/spec/spec.opts +4 -0
  100. data/spec/rails/spec/spec_helper.rb +40 -0
  101. data/spec/rails/spec/views/form_test/form.html.erb_spec.rb +12 -0
  102. data/spec/rails/spec/views/form_test/form_for.html.erb_spec.rb +12 -0
  103. data/spec/rails/spec/views/form_test/form_tag.html.erb_spec.rb +12 -0
  104. data/spec/rcov.opts +2 -0
  105. data/spec/spec.opts +4 -0
  106. data/spec/tractor.rb +48 -0
  107. metadata +190 -0
@@ -0,0 +1,40 @@
1
+
2
+ class Company < ActiveRecord::Base
3
+ has_one :license
4
+ has_many :employees
5
+ has_many :contract_workers
6
+ has_many :contractors, :through=>:contract_workers
7
+ enum_attr :status, %w(s_corp c_corp llc), :nil=>true
8
+ end
9
+ class Employee < ActiveRecord::Base
10
+ belongs_to :company
11
+ enum_attr :status, %w(full_time part_time suspended), :nil=>true
12
+ end
13
+ class License < ActiveRecord::Base
14
+ belongs_to :company
15
+ enum_attr :status, %w(^current expired)
16
+ end
17
+ class Contractor < ActiveRecord::Base
18
+ has_many :contract_workers
19
+ has_many :companies, :through=>:contract_workers
20
+ enum_attr :status, %w(^available unavailable)
21
+ end
22
+ class ContractWorker < ActiveRecord::Base
23
+ belongs_to :company
24
+ belongs_to :contractor
25
+ enum_attr :status, %w(unfresh ^unfresh) #i don't know what to put here
26
+ end
27
+
28
+ #polymorphic
29
+ class Comment < ActiveRecord::Base
30
+ belongs_to :document, :polymorphic=>true
31
+ enum_attr :status, %w(^unflagged flagged)
32
+ end
33
+ class Article < ActiveRecord::Base
34
+ has_one :comment, :as=>:document
35
+ enum_attr :status, %w(^unreviewed accepted)
36
+ end
37
+ class Image < ActiveRecord::Base
38
+ has_one :comment, :as=>:document
39
+ enum_attr :status, %w(^unreviewed accepted)
40
+ end
@@ -0,0 +1,130 @@
1
+ require 'test_in_memory'
2
+ require 'enumerated_attribute'
3
+ require 'active_record'
4
+ require 'association_test_classes'
5
+
6
+ module TestVariables
7
+ def company_name; 'Company A'; end
8
+ end
9
+
10
+ describe "Polymorphic associations" do
11
+ include TestVariables
12
+
13
+ it "should retrieve enum'ed attribute for articles and images of a comment" do
14
+ c1 = Comment.new(:comment=>'i like it')
15
+ c1.document = Article.create!(:name=>'Birds of a Feather', :status=>:accepted)
16
+ c1.save!
17
+ c1.status.should == :unflagged
18
+
19
+ c2 = Comment.new(:comment=>'i hate it', :status => :flagged)
20
+ c2.document = Image.create!(:name=>'Martian Landscape', :status=>:unreviewed)
21
+ c2.save!
22
+ c2.status.should == :flagged
23
+
24
+ Comment.find(c1.id).document.status.should == :accepted
25
+ Comment.find(c2.id).document.status.should == :unreviewed
26
+
27
+ end
28
+
29
+ it "should retrieve enum'ed attribute for comments on articles and images" do
30
+ a = Article.create!(:name=>'Swimming with Whales', :status=>:accepted)
31
+ a.create_comment(:comment=>'i like it', :status=>:unflagged)
32
+ a.save!
33
+
34
+ i = Image.create!(:name=>'Mountain Climbing', :status=>:unreviewed)
35
+ i.comment = Comment.create!(:comment=>'i do not like it', :status=>:flagged)
36
+ i.save!
37
+
38
+ Image.find(i.id).comment.status.should == :flagged
39
+ Article.find(a.id).comment.status.should == :unflagged
40
+
41
+ end
42
+
43
+ end
44
+
45
+ describe "Basic Associations" do
46
+ include TestVariables
47
+
48
+ it "should retrieve enum'ed status for its license" do
49
+ c = Company.new(:status=>:llc, :name=>company_name)
50
+ c.save!
51
+ c.create_license(:status=>:current)
52
+
53
+ r = Company.find(c.id)
54
+ lic = r.license
55
+ r.status.should == :llc
56
+ lic.status.should == :current
57
+ end
58
+
59
+ it "should retrieve enum'ed status for multiple employees" do
60
+ c=Company.new(:status=>:llc, :name=>company_name)
61
+ c.save!
62
+ c.employees.create!(:status=>:full_time, :name=>'edward')
63
+ c.employees.create!(:status=>:suspended, :name=>'tina')
64
+
65
+ r=Company.find(c.id)
66
+ r.employees.find_by_name('edward').status.should == :full_time
67
+ r.employees.find_by_name('tina').status.should == :suspended
68
+ Employee.delete_all
69
+ end
70
+
71
+ it "should retrieve enum'ed status for multiple contractors" do
72
+ c=Company.new(:status=>:llc, :name=>company_name)
73
+ c.save!
74
+ c1 = Contractor.new(:status=>:available, :name=>'john')
75
+ c2 = Contractor.new(:status=>:unavailable, :name=>'sally')
76
+ c.contractors << c1
77
+ c.contractors << c2
78
+
79
+ r=Company.find(c.id)
80
+ r.contractors.find_by_name('sally').status.should == :unavailable
81
+ r.contractors.find_by_name('john').status.should == :available
82
+ Contractor.delete_all
83
+ ContractWorker.delete_all
84
+ end
85
+ end
86
+
87
+ describe "License" do
88
+ include TestVariables
89
+ it "should retrieve the status for the company" do
90
+ Company.delete_all
91
+ License.delete_all
92
+ lic = License.create!(:status=>:expired)
93
+ lic.company = Company.create!(:name=>company_name, :status=>:llc)
94
+ lic.save!
95
+
96
+ License.find(lic.id).company.status.should == :llc
97
+ end
98
+ end
99
+
100
+ describe "Contractor" do
101
+ include TestVariables
102
+ it "should retrieve enum'ed status from multiple companies" do
103
+ Company.delete_all
104
+ Contractor.delete_all
105
+ ContractWorker.delete_all
106
+ c = Contractor.create!(:name=>'jack', :status=>:available)
107
+
108
+ c1 = Company.create!(:name=>company_name, :status=>:s_corp)
109
+ c2 = Company.create!(:name=>'other company', :status=>:llc)
110
+ c.companies << c1 << c2
111
+
112
+ j = Contractor.find(c.id)
113
+ j.companies.find_by_name('other company').status.should == :llc
114
+ j.companies.find_by_name(company_name).status.should == :s_corp
115
+ end
116
+ end
117
+
118
+ describe "Employee" do
119
+ include TestVariables
120
+ it "should retrieve enum'ed status from the company" do
121
+ Company.delete_all
122
+ Employee.delete_all
123
+ e = Employee.create!(:name=>'juanita', :status=>:part_time)
124
+ e.company = Company.create!(:name=>company_name, :status=>:c_corp)
125
+ e.save!
126
+
127
+ emp = Employee.find(e.id)
128
+ emp.company.status.should == :c_corp
129
+ end
130
+ end
@@ -0,0 +1,6 @@
1
+ #irb -r cfg ==> to work configure for working in IRB
2
+ $:.unshift '../../lib'
3
+ require 'test_in_memory'
4
+ require 'enumerated_attribute'
5
+ require 'race_car'
6
+ require 'sti_classes'
@@ -0,0 +1,12 @@
1
+ require 'cfg'
2
+
3
+ r=RaceCar.new
4
+ r.gear = :second
5
+ r.name = 'special'
6
+ r.save!
7
+
8
+ #RaceCar.find_by_gear_and_name(:second, 'special')
9
+ car = RaceCar.find_or_create_by_name_and_gear('special', :second)
10
+ debugger
11
+ car0 = RaceCar.find_or_create_by_name_and_gear('special', :second)
12
+ car0
@@ -0,0 +1,19 @@
1
+ require 'race_car'
2
+
3
+ class SubRaceCar < RaceCar
4
+ set_table_name "race_cars"
5
+
6
+ enum_attr :extra, %w(^one two three four)
7
+ end
8
+
9
+ class SubRaceCar2 < RaceCar
10
+ set_table_name "race_cars"
11
+
12
+ end
13
+
14
+ class SubRaceCar3 < RaceCar
15
+ set_table_name "race_cars"
16
+
17
+ enum_attr :gear, %w(reverse neutral ^first second third over_drive)
18
+ end
19
+
@@ -0,0 +1,60 @@
1
+ require 'test_in_memory'
2
+ require 'enumerated_attribute'
3
+ require 'active_record'
4
+ require 'active_record/inheritance_classes'
5
+
6
+ def one_to_four; [:one, :two, :three, :four]; end
7
+ def gear1; [:reverse, :neutral, :first, :second, :over_drive]; end
8
+ def gear2; [:reverse, :neutral, :first, :second, :third, :over_drive]; end
9
+ def choke; [:none, :medium, :full]; end
10
+
11
+ describe "SubRaceCar" do
12
+ it "should have :gear init to :neutral" do
13
+ o=SubRaceCar.new
14
+ o.gear.should == :neutral
15
+ o.enums(:gear).should == gear1
16
+ o.gear_previous.should == :reverse
17
+ end
18
+ it "should have :choke init to :none" do
19
+ o=SubRaceCar.new
20
+ o.choke.should == :none
21
+ o.enums(:choke).should == choke
22
+ o.choke_previous.should == :full
23
+ end
24
+ it "should have :extra init to :one" do
25
+ o=SubRaceCar.new
26
+ o.extra.should == :one
27
+ o.enums(:extra).should == one_to_four
28
+ o.extra_previous.should == :four
29
+ end
30
+ end
31
+
32
+ describe "SubRaceCar2" do
33
+ it "should have :gear init to :neutral" do
34
+ o=SubRaceCar2.new
35
+ o.gear.should == :neutral
36
+ o.enums(:gear).should == gear1
37
+ o.gear_previous.should == :reverse
38
+ end
39
+ it "should have :choke init to :none" do
40
+ o=SubRaceCar2.new
41
+ o.choke.should == :none
42
+ o.enums(:choke).should == choke
43
+ o.choke_previous.should == :full
44
+ end
45
+ end
46
+
47
+ describe "SubRaceCar3" do
48
+ it "should have overridden :gear init to :first" do
49
+ o=SubRaceCar3.new
50
+ o.gear.should == :first
51
+ o.enums(:gear).should == gear2
52
+ o.gear_previous.should == :neutral
53
+ end
54
+ it "should have :choke init to :none" do
55
+ o=SubRaceCar3.new
56
+ o.choke.should == :none
57
+ o.enums(:choke).should == choke
58
+ o.choke_previous.should == :full
59
+ end
60
+ end
@@ -0,0 +1,15 @@
1
+
2
+ class RaceCar < ActiveRecord::Base
3
+ enum_attr :gear, %w(reverse ^neutral first second over_drive)
4
+ enum_attr :choke, %w(^none medium full)
5
+ end
6
+
7
+ #gear = enumerated column attribute
8
+ #choke = enumerated non-column attribute
9
+ #lights = non-enumerated column attribute
10
+
11
+ =begin
12
+ t.string :name
13
+ t.string :gear
14
+ t.string :lights
15
+ =end
@@ -0,0 +1,10 @@
1
+
2
+ class StiParent < ActiveRecord::Base
3
+ enum_attr :parent_enum, %w(p1 p2 p3)
4
+
5
+ end
6
+
7
+ class StiSub < StiParent
8
+ enum_attr :sub_enum, %w(s1 s2 s3)
9
+ end
10
+
@@ -0,0 +1,41 @@
1
+ require 'test_in_memory'
2
+ require 'enumerated_attribute'
3
+ require 'active_record'
4
+ require 'active_record/sti_classes'
5
+
6
+
7
+ #contributed by Rob Chekaluk
8
+
9
+ describe "enum_attr for STI subclass" do
10
+ it "should assign, save and retrieve non-enumerated attribute" do
11
+ s = StiSub.new()
12
+ s.sub_nonenum = "value2"
13
+ s.save
14
+ s0 = StiSub.find(s.id)
15
+ s0.sub_nonenum.should == "value2"
16
+ end
17
+ it "should assign, save and retrieve subclass enum" do
18
+ s = StiSub.new()
19
+ s.sub_enum = :s3
20
+ s.sub_enum.should == :s3
21
+ s.save
22
+ s0 = StiSub.find(s.id)
23
+ s0.sub_enum.should == :s3
24
+ end
25
+ it "should assign, save and retrieve inherited enum" do
26
+ s = StiSub.new()
27
+ s.parent_enum = :p2
28
+ s.parent_enum.should == :p2
29
+ s.save
30
+ s0 = StiSub.find(s.id)
31
+ s0.parent_enum.should == :p2
32
+ end
33
+ it "should use predicate methods to access enumerated attributes" do
34
+ s=StiSub.create(:parent_enum=>:p2, :sub_enum=>:s1)
35
+ s.parent_enum_is_p2?.should be_true
36
+ s.parent_enum_is_not_p2?.should be_false
37
+ s.sub_enum_is_s2?.should be_false
38
+ s.sub_enum_is_not_s2?.should be_true
39
+ end
40
+ end
41
+
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+
3
+ gem 'activerecord', ENV['AR_VERSION'] ? "=#{ENV['AR_VERSION']}" : '>=2.1.0'
4
+ require 'active_record'
5
+ require 'active_support/core_ext/logger' rescue nil # rails3
6
+
7
+ require 'enumerated_attribute'
8
+
9
+ ActiveRecord::Base.establish_connection({'adapter' => 'sqlite3', 'database' => ':memory:'})
10
+ ActiveRecord::Base.logger = Logger.new("#{File.dirname(__FILE__)}/active_record.log")
11
+
12
+ connection = ActiveRecord::Base.connection
13
+ connection.create_table(:race_cars, :force=>true) do |t|
14
+ t.string :name
15
+ t.enum :gear
16
+ t.enum :lights
17
+ t.timestamps
18
+ end
19
+ connection.create_table(:bicycles, :force=>true) do |t|
20
+ t.string :name
21
+ t.enum :speed
22
+ t.enum :gear
23
+ end
24
+
25
+ #basic_associations
26
+ connection.create_table(:companies, :force=>true) do |t|
27
+ t.string :name
28
+ t.string :status
29
+ end
30
+ connection.create_table(:contract_workers, :force=>true) do |t|
31
+ t.references :company
32
+ t.references :contractor
33
+ t.string :status
34
+ end
35
+ connection.create_table(:licenses, :force=>true) do |t|
36
+ t.references :company
37
+ t.string :status
38
+ end
39
+ connection.create_table(:contractors, :force=>true) do |t|
40
+ t.string :name
41
+ t.string :status
42
+ end
43
+ connection.create_table(:employees, :force=>true) do |t|
44
+ t.references :company
45
+ t.string :name
46
+ t.string :status
47
+ end
48
+
49
+ #polymorphic_associations
50
+ connection.create_table(:comments, :force=>true) do |t|
51
+ t.references :document, :polymorphic=>true
52
+ t.text :comment
53
+ t.string :status
54
+ end
55
+ connection.create_table(:articles, :force=>true) do |t|
56
+ t.string :name
57
+ t.string :status
58
+ end
59
+ connection.create_table(:images, :force=>true) do |t|
60
+ t.string :name
61
+ t.string :status
62
+ end
63
+
64
+ #single table inheritance
65
+ connection.create_table(:sti_parents, :force=>true) do |t|
66
+ t.string :type
67
+ t.enum :parent_enum
68
+ t.string :sub_nonenum
69
+ t.enum :sub_enum
70
+ t.timestamps
71
+ end
@@ -0,0 +1,67 @@
1
+ require 'enumerated_attribute'
2
+
3
+ #used to test that method_missing chaining plays nice in inheritance situations
4
+
5
+ class Vehicle
6
+ def vehicle_method_missing_called?; @vehicle_method_missing_called; end
7
+ def self.vehicle_new_called?; @@vehicle_new_called; end
8
+ def self.reset; @@vehicle_new_called = false; end
9
+
10
+ @@vehicle_new_called = false
11
+ def initialize
12
+ @vehicle_method_missing_called = false
13
+ super
14
+ end
15
+
16
+ def method_missing(methId, *args)
17
+ @vehicle_method_missing_called = true
18
+ #end here
19
+ end
20
+
21
+ def self.new
22
+ @@vehicle_new_called = true
23
+ super
24
+ end
25
+
26
+ end
27
+
28
+ class CarWithMethods < Vehicle
29
+ def car_method_missing_called?; @car_method_missing_called; end
30
+ def self.car_new_called?; @@car_new_called; end
31
+ def self.reset; @@car_new_called = false; super; end
32
+
33
+ def self.new
34
+ @@car_new_called = true
35
+ super
36
+ end
37
+
38
+ def method_missing(methId, *args)
39
+ @car_method_missing_called = true
40
+ super
41
+ end
42
+
43
+ enum_attr :gear, %w(reverse ^neutral drive)
44
+
45
+ @@car_new_called = false
46
+ def initialize
47
+ @car_method_missing_called = false
48
+ super
49
+ end
50
+
51
+ end
52
+
53
+ class CarWithoutMethods < Vehicle
54
+ def car_method_missing_called?; @car_method_missing_called; end
55
+ def self.car_new_called?; @@car_new_called; end
56
+ def self.reset; @@car_new_called = false; super; end
57
+
58
+ enum_attr :gear, %w(reverse ^neutral drive)
59
+
60
+ @@car_new_called = false
61
+ def initialize
62
+ @car_method_missing_called = false
63
+ super
64
+ end
65
+
66
+ end
67
+