jeffp-enumerated_attribute 0.1.7 → 0.2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -1
- data/README.rdoc +146 -32
- data/Rakefile +29 -14
- data/lib/enumerated_attribute.rb +0 -1
- data/lib/enumerated_attribute/attribute.rb +28 -26
- data/lib/enumerated_attribute/attribute/attribute_descriptor.rb +50 -0
- data/lib/enumerated_attribute/integrations/active_record.rb +6 -3
- data/lib/enumerated_attribute/integrations/object.rb +2 -1
- data/lib/enumerated_attribute/method_definition_dsl.rb +21 -11
- data/lib/enumerated_attribute/rails_helpers.rb +73 -0
- data/spec/active_record/active_record_spec.rb +49 -2
- data/spec/active_record/association_test_classes.rb +40 -0
- data/spec/active_record/associations_spec.rb +130 -0
- data/spec/active_record/test_in_memory.rb +40 -0
- data/spec/poro_spec.rb +32 -4
- data/spec/rails/README +243 -0
- data/spec/rails/Rakefile +10 -0
- data/spec/rails/app/controllers/application_controller.rb +10 -0
- data/spec/rails/app/controllers/form_test_controller.rb +38 -0
- data/spec/rails/app/helpers/application_helper.rb +3 -0
- data/spec/rails/app/helpers/form_test_helper.rb +2 -0
- data/spec/rails/app/models/user.rb +9 -0
- data/spec/rails/app/views/form_test/form.html.erb +1 -0
- data/spec/rails/app/views/form_test/form_for.html.erb +10 -0
- data/spec/rails/app/views/form_test/form_tag.html.erb +9 -0
- data/spec/rails/app/views/form_test/index.html.erb +6 -0
- data/spec/rails/app/views/layouts/application.html.erb +11 -0
- data/spec/rails/config/boot.rb +110 -0
- data/spec/rails/config/database.yml +22 -0
- data/spec/rails/config/environment.rb +45 -0
- data/spec/rails/config/environments/development.rb +17 -0
- data/spec/rails/config/environments/production.rb +28 -0
- data/spec/rails/config/environments/test.rb +28 -0
- data/spec/rails/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rails/config/initializers/inflections.rb +10 -0
- data/spec/rails/config/initializers/mime_types.rb +5 -0
- data/spec/rails/config/initializers/new_rails_defaults.rb +19 -0
- data/spec/rails/config/initializers/session_store.rb +15 -0
- data/spec/rails/config/locales/en.yml +5 -0
- data/spec/rails/config/routes.rb +43 -0
- data/spec/rails/db/development.sqlite3 +0 -0
- data/spec/rails/db/migrate/20090804230221_create_sessions.rb +16 -0
- data/spec/rails/db/migrate/20090804230546_create_users.rb +21 -0
- data/spec/rails/db/schema.rb +35 -0
- data/spec/rails/db/test.sqlite3 +0 -0
- data/spec/rails/public/404.html +30 -0
- data/spec/rails/public/422.html +30 -0
- data/spec/rails/public/500.html +30 -0
- data/spec/rails/public/favicon.ico +0 -0
- data/spec/rails/public/images/rails.png +0 -0
- data/spec/rails/public/index.html +275 -0
- data/spec/rails/public/javascripts/application.js +2 -0
- data/spec/rails/public/javascripts/controls.js +963 -0
- data/spec/rails/public/javascripts/dragdrop.js +973 -0
- data/spec/rails/public/javascripts/effects.js +1128 -0
- data/spec/rails/public/javascripts/prototype.js +4320 -0
- data/spec/rails/public/robots.txt +5 -0
- data/spec/rails/public/stylesheets/scaffold.css +54 -0
- data/spec/rails/script/about +4 -0
- data/spec/rails/script/autospec +6 -0
- data/spec/rails/script/console +3 -0
- data/spec/rails/script/dbconsole +3 -0
- data/spec/rails/script/destroy +3 -0
- data/spec/rails/script/generate +3 -0
- data/spec/rails/script/performance/benchmarker +3 -0
- data/spec/rails/script/performance/profiler +3 -0
- data/spec/rails/script/plugin +3 -0
- data/spec/rails/script/runner +3 -0
- data/spec/rails/script/server +3 -0
- data/spec/rails/script/spec +10 -0
- data/spec/rails/script/spec_server +9 -0
- data/spec/rails/spec/controllers/form_test_controller_spec.rb +41 -0
- data/spec/rails/spec/integrations/enum_select_spec.rb +73 -0
- data/spec/rails/spec/matchers.rb +12 -0
- data/spec/rails/spec/rcov.opts +2 -0
- data/spec/rails/spec/spec.opts +4 -0
- data/spec/rails/spec/spec_helper.rb +38 -0
- data/spec/rails/spec/views/form_test/form.html.erb_spec.rb +12 -0
- data/spec/rails/spec/views/form_test/form_for.html.erb_spec.rb +12 -0
- data/spec/rails/spec/views/form_test/form_tag.html.erb_spec.rb +12 -0
- data/spec/rcov.opts +2 -0
- data/spec/spec.opts +4 -0
- data/spec/tractor.rb +4 -1
- metadata +103 -5
@@ -0,0 +1,50 @@
|
|
1
|
+
|
2
|
+
module EnumeratedAttribute
|
3
|
+
module Attribute
|
4
|
+
class AttributeDescriptor < Array
|
5
|
+
attr_reader :name
|
6
|
+
|
7
|
+
def initialize(name, enums=[], opts={})
|
8
|
+
super enums
|
9
|
+
@name = name
|
10
|
+
@options = opts
|
11
|
+
@labels_hash = Hash[*self.collect{|e| [e, e.to_s.gsub(/_/, ' ').squeeze(' ').capitalize]}.flatten]
|
12
|
+
end
|
13
|
+
|
14
|
+
def allows_nil?
|
15
|
+
@options.key?(:nil) ? @options[:nil] : true
|
16
|
+
end
|
17
|
+
def allows_value?(value)
|
18
|
+
self.include?(value.to_sym)
|
19
|
+
end
|
20
|
+
|
21
|
+
def enums
|
22
|
+
self
|
23
|
+
end
|
24
|
+
def label(value)
|
25
|
+
@labels_hash[value]
|
26
|
+
end
|
27
|
+
def labels
|
28
|
+
@labels_array ||= self.map{|e| @labels_hash[e]}
|
29
|
+
end
|
30
|
+
def hash
|
31
|
+
@labels_hash
|
32
|
+
end
|
33
|
+
def select_options
|
34
|
+
@select_options ||= self.map{|e| [@labels_hash[e], e.to_s]}
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_label(enum_value, label_string)
|
38
|
+
reset_labels
|
39
|
+
@labels_hash[enum_value.to_sym] = label_string
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
def reset_labels
|
44
|
+
@labels_array = nil
|
45
|
+
@select_options = nil
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -9,6 +9,7 @@ module EnumeratedAttribute
|
|
9
9
|
def write_enumerated_attribute(name, val)
|
10
10
|
name = name.to_s
|
11
11
|
return write_attribute(name, val) unless self.class.has_enumerated_attribute?(name)
|
12
|
+
val = nil if val == ''
|
12
13
|
val_str = val.to_s if val
|
13
14
|
val_sym = val.to_sym if val
|
14
15
|
unless self.class.enumerated_attribute_allows_value?(name, val_sym)
|
@@ -17,6 +18,7 @@ module EnumeratedAttribute
|
|
17
18
|
end
|
18
19
|
return instance_variable_set('@'+name, val_sym) unless self.has_attribute?(name)
|
19
20
|
write_attribute(name, val_str)
|
21
|
+
val_sym
|
20
22
|
end
|
21
23
|
|
22
24
|
def read_enumerated_attribute(name)
|
@@ -24,10 +26,11 @@ module EnumeratedAttribute
|
|
24
26
|
#if not enumerated - let active record handle it
|
25
27
|
return read_attribute(name) unless self.class.has_enumerated_attribute?(name)
|
26
28
|
#if enumerated, is it an active record attribute, if not, the value is stored in an instance variable
|
29
|
+
name_sym = name.to_sym
|
27
30
|
return instance_variable_get('@'+name) unless self.has_attribute?(name)
|
28
31
|
#this is an enumerated active record attribute
|
29
32
|
val = read_attribute(name)
|
30
|
-
val = val.to_sym if
|
33
|
+
val = val.to_sym if !!val
|
31
34
|
val
|
32
35
|
end
|
33
36
|
|
@@ -48,7 +51,7 @@ module EnumeratedAttribute
|
|
48
51
|
|
49
52
|
def attributes
|
50
53
|
super.map do |k,v|
|
51
|
-
self.class.has_enumerated_attribute?(k) ? v
|
54
|
+
self.class.has_enumerated_attribute?(k) ? @@enumerated_attributes[k].cast_enum_symbol(v) : v
|
52
55
|
end
|
53
56
|
end
|
54
57
|
|
@@ -66,7 +69,7 @@ module EnumeratedAttribute
|
|
66
69
|
attributes = super
|
67
70
|
attributes.each { |k,v| attributes[k] = v.to_s if has_enumerated_attribute?(k) }
|
68
71
|
attributes
|
69
|
-
end
|
72
|
+
end
|
70
73
|
|
71
74
|
def instantiate(record)
|
72
75
|
object = super(record)
|
@@ -8,6 +8,7 @@ module EnumeratedAttribute
|
|
8
8
|
|
9
9
|
def write_enumerated_attribute(name, val)
|
10
10
|
name = name.to_s
|
11
|
+
val = nil if val == ''
|
11
12
|
val = val.to_sym if val
|
12
13
|
unless self.class.enumerated_attribute_allows_value?(name, val)
|
13
14
|
raise(InvalidEnumeration, "nil is not allowed on '#{name}' attribute, set :nil=>true option", caller) unless val
|
@@ -17,7 +18,7 @@ module EnumeratedAttribute
|
|
17
18
|
end
|
18
19
|
|
19
20
|
def read_enumerated_attribute(name)
|
20
|
-
|
21
|
+
instance_variable_get('@'+name.to_s)
|
21
22
|
end
|
22
23
|
|
23
24
|
module ClassMethods
|
@@ -15,10 +15,10 @@ module EnumeratedAttribute
|
|
15
15
|
class MethodDefinitionDSL
|
16
16
|
attr_reader :initial_value, :pluralized_name, :decrementor_name, :incrementor_name
|
17
17
|
|
18
|
-
def initialize(class_obj,
|
18
|
+
def initialize(class_obj, descriptor)
|
19
19
|
@class_obj = class_obj
|
20
|
-
@attr_name =
|
21
|
-
@
|
20
|
+
@attr_name = descriptor.name
|
21
|
+
@attr_descriptor = descriptor #this is the enums array
|
22
22
|
end
|
23
23
|
|
24
24
|
#we'll by pass this - they can use it if it helps make code more readable - not enforced - should it be??
|
@@ -60,7 +60,7 @@ module EnumeratedAttribute
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def init(value)
|
63
|
-
if (!@
|
63
|
+
if (!@attr_descriptor.empty? && !@attr_descriptor.include?(value.to_sym))
|
64
64
|
raise(InvalidDefinition, "'#{value}' in method 'init' is not an enumeration value for :#{@attr_name} attribute", caller)
|
65
65
|
end
|
66
66
|
@initial_value = value
|
@@ -69,10 +69,20 @@ module EnumeratedAttribute
|
|
69
69
|
def decrementor(value); @decrementor_name = value; end
|
70
70
|
def incrementor(value); @incrementor_name = value; end
|
71
71
|
def enums_accessor(value); @pluralized_name = value; end
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
72
|
+
alias_method :inc, :incrementor
|
73
|
+
alias_method :dec, :decrementor
|
74
|
+
alias_method :enums, :enums_accessor
|
75
|
+
alias_method :plural, :enums_accessor
|
76
|
+
|
77
|
+
def label(hash)
|
78
|
+
raise(InvalidDefinition, "label or labels keyword should be followed by a hash of :enum_value=>'label'", caller) unless hash.is_a?(Hash)
|
79
|
+
hash.each do |k,v|
|
80
|
+
raise(InvalidDefinition, "#{k} is not an enumeration value for :#{@attr_name} attribute", caller) unless (k.is_a?(Symbol) && @attr_descriptor.include?(k))
|
81
|
+
raise(InvalidDefinition, "#{v} is not a string. Labels should be strings", caller) unless v.is_a?(String)
|
82
|
+
@attr_descriptor.set_label(k, v)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
alias_method :labels, :label
|
76
86
|
|
77
87
|
private
|
78
88
|
|
@@ -111,16 +121,16 @@ module EnumeratedAttribute
|
|
111
121
|
end
|
112
122
|
|
113
123
|
def create_custom_method_for_symbol_or_string(mdef)
|
114
|
-
if (!@
|
124
|
+
if (!@attr_descriptor.empty? && !@attr_descriptor.include?(mdef.argument.to_sym))
|
115
125
|
raise(InvalidDefinition, "'#{mdef.argument}' in method '#{mdef.method_name}' is not an enumeration value for :#{@attr_name} attribute", caller)
|
116
126
|
end
|
117
127
|
@class_obj.class_eval("def #{mdef.method_name}; @#{@attr_name} #{mdef.negated ? '!=' : '=='} :#{mdef.argument}; end")
|
118
128
|
end
|
119
129
|
|
120
130
|
def create_custom_method_for_array_of_enums(mdef)
|
121
|
-
if !@
|
131
|
+
if !@attr_descriptor.empty?
|
122
132
|
mdef.argument.each do |m|
|
123
|
-
if !@
|
133
|
+
if !@attr_descriptor.include?(m.to_sym)
|
124
134
|
raise(InvalidDefinition, "'#{m}' in method '#{mdef.method_name}' is not an enumeration value for :#{@attr_name} attribute", caller)
|
125
135
|
end
|
126
136
|
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
|
2
|
+
if defined?(ActiveRecord)
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
class TableDefinition
|
6
|
+
def column_with_enumerated_attribute(name, type, options = {})
|
7
|
+
type = 'string' if type.to_s == 'enum'
|
8
|
+
column_without_enumerated_attribute(name, type, options)
|
9
|
+
end
|
10
|
+
alias_method_chain :column, :enumerated_attribute
|
11
|
+
|
12
|
+
def enum(*args)
|
13
|
+
options = args.extract_options!
|
14
|
+
column_names = args
|
15
|
+
column_names.each { |name| column(name, 'string', options) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
if defined?(ActionView::Base)
|
23
|
+
module ActionView
|
24
|
+
module Helpers
|
25
|
+
|
26
|
+
#form_options_helper.rb
|
27
|
+
module FormOptionsHelper
|
28
|
+
#def select
|
29
|
+
def enum_select(object, method, options={}, html_options={})
|
30
|
+
InstanceTag.new(object, method, self, options.delete(:object)).to_enum_select_tag(options, html_options)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class InstanceTag
|
35
|
+
def to_enum_select_tag(options, html_options={})
|
36
|
+
choices = []
|
37
|
+
if self.object.respond_to?(:enums)
|
38
|
+
enums = self.object.enums(method_name.to_sym)
|
39
|
+
choices = enums ? enums.select_options : []
|
40
|
+
if (value = self.object.__send__(method_name.to_sym))
|
41
|
+
options[:selected] ||= value.to_s
|
42
|
+
else
|
43
|
+
options[:include_blank] ||= true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
to_select_tag(choices, options, html_options)
|
47
|
+
end
|
48
|
+
|
49
|
+
#initialize record_name, method, self
|
50
|
+
def to_tag_with_enumerated_attribute(options={})
|
51
|
+
#look for an enum
|
52
|
+
if (column_type == :string &&
|
53
|
+
self.object.class.respond_to?(:has_enumerated_attribute?) &&
|
54
|
+
self.object.class.has_enumerated_attribute?(method_name.to_sym))
|
55
|
+
to_enum_select_tag(options)
|
56
|
+
else
|
57
|
+
to_tag_without_enumerated_attribute(options)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
alias_method_chain :to_tag, :enumerated_attribute
|
61
|
+
end
|
62
|
+
|
63
|
+
class FormBuilder
|
64
|
+
def enum_select(method, options={}, html_options={})
|
65
|
+
@template.enum_select(@object_name, method, objectify_options(options), @default_options.merge(html_options))
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
@@ -1,9 +1,48 @@
|
|
1
1
|
require 'test_in_memory'
|
2
|
-
require 'enumerated_attribute'
|
3
2
|
require 'active_record'
|
3
|
+
require 'enumerated_attribute'
|
4
4
|
require 'race_car'
|
5
5
|
|
6
6
|
describe "RaceCar" do
|
7
|
+
|
8
|
+
it "should have default labels for :gear attribute" do
|
9
|
+
labels_hash = {:reverse=>'Reverse', :neutral=>'Neutral', :first=>'First', :second=>'Second', :over_drive=>'Over drive'}
|
10
|
+
labels = ['Reverse', 'Neutral', 'First', 'Second', 'Over drive']
|
11
|
+
select_options = [['Reverse', 'reverse'], ['Neutral', 'neutral'], ['First', 'first'], ['Second', 'second'], ['Over drive', 'over_drive']]
|
12
|
+
r=RaceCar.new
|
13
|
+
r.gears.labels.should == labels
|
14
|
+
labels_hash.each do |k,v|
|
15
|
+
r.gears.label(k).should == v
|
16
|
+
end
|
17
|
+
r.gears.hash.should == labels_hash
|
18
|
+
r.gears.select_options.should == select_options
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should retrieve :gear enums through enums method" do
|
22
|
+
r=RaceCar.new
|
23
|
+
r.enums(:gear).should == r.gears
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return a Symbol type from reader methods" do
|
27
|
+
r=RaceCar.new
|
28
|
+
r.gear.should be_an_instance_of(Symbol)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should increment and decrement :gear attribute correctly" do
|
32
|
+
r=RaceCar.new
|
33
|
+
r.gear = :neutral
|
34
|
+
r.gear_next.should == :first
|
35
|
+
r.gear_next.should == :second
|
36
|
+
r.gear_next.should == :over_drive
|
37
|
+
r.gear_next.should == :reverse
|
38
|
+
r.gear_next.should == :neutral
|
39
|
+
r.gear.should == :neutral
|
40
|
+
r.gear_previous.should == :reverse
|
41
|
+
r.gear_previous.should == :over_drive
|
42
|
+
r.gear_previous.should == :second
|
43
|
+
r.gear_previous
|
44
|
+
r.gear.should == :first
|
45
|
+
end
|
7
46
|
|
8
47
|
it "should have dynamic predicate methods for :gear attribute" do
|
9
48
|
r=RaceCar.new
|
@@ -55,13 +94,21 @@ describe "RaceCar" do
|
|
55
94
|
r.choke.should == :medium
|
56
95
|
end
|
57
96
|
|
58
|
-
it "should initialize using parameter hash" do
|
97
|
+
it "should initialize using parameter hash with symbol keys" do
|
59
98
|
r=RaceCar.new(:name=>'FastFurious', :gear=>:second, :lights=>'on', :choke=>:medium)
|
60
99
|
r.gear.should == :second
|
61
100
|
r.lights.should == 'on'
|
62
101
|
r.choke.should == :medium
|
63
102
|
end
|
64
103
|
|
104
|
+
it "should initialize using parameter hash with string keys" do
|
105
|
+
r=RaceCar.new({'name'=>'FastFurious', 'gear'=>'second', 'lights'=>'on', 'choke'=>'medium'})
|
106
|
+
r.gear.should == :second
|
107
|
+
r.lights.should == 'on'
|
108
|
+
r.choke.should == :medium
|
109
|
+
end
|
110
|
+
|
111
|
+
|
65
112
|
it "should convert non-column enumerated attributes from string to symbols" do
|
66
113
|
r=RaceCar.new
|
67
114
|
r.choke = 'medium'
|
@@ -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
|