affairs_of_state 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ spec/db/test.sqlite3
19
+
data/README.md CHANGED
@@ -51,6 +51,12 @@ You can also access all your statuses on the model like so:
51
51
 
52
52
  Widget::STATUES # -> ["active", "cancelled"]
53
53
 
54
+ It also provides scopes automagically:
55
+
56
+ Widget.active
57
+
58
+ Widget.cancelled
59
+
54
60
 
55
61
  ## "But I want callbacks and validations and other things."
56
62
 
@@ -8,12 +8,18 @@ module AffairsOfState
8
8
 
9
9
  module ClassMethods
10
10
  def affairs_of_state(*args)
11
- @_status_options = ({:column => :status, :allow_blank => false}).merge(args.extract_options!)
12
- @_statuses = args.map(&:to_s)
11
+ @_status_options = ({:column => :status, :allow_blank => false, :scopes => true}).merge(args.extract_options!)
12
+ @_statuses = args.flatten.map(&:to_s)
13
13
 
14
14
  const_set("STATUSES", @_statuses)
15
15
 
16
- validates(@_status_options[:column], :presence => true) unless @_status_options[:allow_blank]
16
+ validates(@_status_options[:column], :inclusion => {:in => @_statuses, :allow_blank => @_status_options[:allow_blank]})
17
+
18
+ if @_status_options[:scopes]
19
+ @_statuses.each do |status|
20
+ scope status.to_sym, where(@_status_options[:column] => status.to_s)
21
+ end
22
+ end
17
23
 
18
24
  include InstanceMethods
19
25
  extend SingletonMethods
@@ -1,3 +1,3 @@
1
1
  module AffairsOfState
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -16,6 +16,11 @@ describe AffairsOfState do
16
16
  p.should_not be_valid
17
17
  end
18
18
 
19
+ it "should validate that we're not setting it to something stupid" do
20
+ p = Pie.new :status => "delicious_pie"
21
+ p.should_not be_valid
22
+ end
23
+
19
24
  describe "boolean methods" do
20
25
  it "should find the set status" do
21
26
  p = Pie.new :status => "active"
@@ -39,6 +44,23 @@ describe AffairsOfState do
39
44
  it "should provide a method to pass to dropdowns" do
40
45
  Pie.statuses_for_select.should == [["Active", "active"], ["Inactive", "inactive"], ["Cancelled", "cancelled"]]
41
46
  end
47
+
48
+ describe "scopes" do
49
+ it "should have a finder to match the status name" do
50
+ Pie.create! :status => "active"
51
+ Pie.create! :status => "inactive"
52
+ Pie.create! :status => "active"
53
+ Pie.create! :status => "cancelled"
54
+
55
+ Pie.active.size.should == 2
56
+ Pie.inactive.size.should == 1
57
+ Pie.cancelled.size.should == 1
58
+ end
59
+ end
60
+
61
+ after(:each) do
62
+ Pie.destroy_all
63
+ end
42
64
  end
43
65
 
44
66
  describe "with a non-standard column name" do
@@ -67,4 +89,28 @@ describe AffairsOfState do
67
89
  end
68
90
  end
69
91
 
92
+ describe "with an array rather than *args" do
93
+ class Pie4 < ActiveRecord::Base
94
+ self.table_name = "pies"
95
+
96
+ affairs_of_state [:on, :off]
97
+ end
98
+
99
+ it "should work too if that's what floats your boat" do
100
+ Pie4::STATUSES.should == ["on", "off"]
101
+ end
102
+ end
103
+
104
+ describe "without the scopes" do
105
+ class Pie5 < ActiveRecord::Base
106
+ self.table_name = "pies"
107
+
108
+ affairs_of_state :active, :inactive, :scopes => false
109
+ end
110
+
111
+ it "should work too if that's what floats your boat" do
112
+ Pie5.should_not respond_to(:active)
113
+ end
114
+ end
115
+
70
116
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: affairs_of_state
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-05-31 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &75399850 !ruby/object:Gem::Requirement
16
+ requirement: &84021290 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *75399850
24
+ version_requirements: *84021290
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &75399640 !ruby/object:Gem::Requirement
27
+ requirement: &84021080 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *75399640
35
+ version_requirements: *84021080
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &75399410 !ruby/object:Gem::Requirement
38
+ requirement: &84020850 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *75399410
46
+ version_requirements: *84020850
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: pry
49
- requirement: &75399200 !ruby/object:Gem::Requirement
49
+ requirement: &84020640 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *75399200
57
+ version_requirements: *84020640
58
58
  description: Add a simple state to a gem, without all the hassle of a complex state
59
59
  machine.
60
60
  email:
@@ -74,7 +74,6 @@ files:
74
74
  - lib/affairs_of_state/version.rb
75
75
  - spec/affairs_of_state_spec.rb
76
76
  - spec/db/.gitkeep
77
- - spec/db/test.sqlite3
78
77
  - spec/spec_helper.rb
79
78
  homepage: http://github.com/kimos/affairs_of_state
80
79
  licenses: []
data/spec/db/test.sqlite3 DELETED
Binary file