affairs_of_state 0.2.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 71da77675fa54352c44a00554fdde62d56b50881
4
- data.tar.gz: d2a0215074c06bc571b51d8b0e918696ec1b027f
2
+ SHA256:
3
+ metadata.gz: e6950061957896a9e0bf23635b6b97d75c97fd09bf52eb51fa1fb30352842a00
4
+ data.tar.gz: 8521df3d7183885b795982184e07adb4492a085f2fc46d0457af8f61f16d8f6b
5
5
  SHA512:
6
- metadata.gz: 777b4d67fde6f03c6ce0320d23ec7879d9dce2f9da70b026d1d70d1c78f8cb15c4c90393e381c46b1fc0935cde20b3f5a6122794fd93b2b81b9a9d08d5bb6b22
7
- data.tar.gz: edd41d5cace467b7d629ee7d1596be76491272f6e2f66208a8bb9bb05dcc1ad05f4d10ec4920071a762123b415d834ad2ca9f5715dfe45559ea0297755053083
6
+ metadata.gz: 637f0bd6245bfc644145f0640fd690231a729b6a6557c034ccf0b8a07582e388335f6e49c9637428c739c4edbf15ccf19d867bb9a45ca87f0fbe5a5412e7f31f
7
+ data.tar.gz: 9cfb5bff07dea9c39183c89f4469df774dc1470997707eff32b26d725bffa35a7e129dbf72369ef7557b44f0d55b805fce7fe9c8dcec44e5094fc2b88e6ed7e3
data/README.md CHANGED
@@ -60,6 +60,8 @@ or
60
60
  affairs_of_state :active, :inactive, if: :only_validate_if_this_method_returns_true
61
61
  ```
62
62
 
63
+ Currently it is limited only be called once per model.
64
+
63
65
 
64
66
  ## Methods
65
67
 
@@ -22,5 +22,6 @@ Gem::Specification.new do |gem|
22
22
  gem.add_development_dependency "rspec"
23
23
  gem.add_development_dependency "sqlite3"
24
24
  gem.add_development_dependency "pry"
25
+ gem.add_development_dependency "rake"
25
26
 
26
27
  end
@@ -1,21 +1,41 @@
1
+ require "active_support"
2
+ require "active_support/concern"
3
+ require "active_record"
4
+
1
5
  require "affairs_of_state/version"
6
+ require "affairs_of_state/config"
2
7
 
3
8
  module AffairsOfState
4
9
  extend ActiveSupport::Concern
5
10
 
6
11
  class_methods do
7
- def affairs_of_state(*args)
8
- @_status_options = { column: :status, allow_blank: false, scopes: true, if: nil }.merge(args.extract_options!)
9
- @_statuses = args.flatten.map(&:to_s)
12
+ def affairs_of_state(*statuses, column: :status, allow_blank: false, scopes: true, if: nil)
13
+ raise ArgumentError.new("Affairs of State: cannot be invoked multiple times on the same model") if @affairs_of_state_config
14
+
15
+ affairs_of_state_config.statuses = statuses
16
+ affairs_of_state_config.column = column
17
+ affairs_of_state_config.allow_blank = allow_blank
18
+ affairs_of_state_config.scopes = scopes
19
+ affairs_of_state_config.if = binding.local_variable_get(:if)
20
+
21
+ const_set(:STATUSES, affairs_of_state_config.statuses)
22
+
23
+ affairs_of_state_config.statuses.each do |status|
24
+ define_method("#{ status }?") do
25
+ self.send(self.class.affairs_of_state_config.column) == status
26
+ end
10
27
 
11
- const_set("STATUSES", @_statuses)
28
+ define_method("#{ status }!") do
29
+ self.send("#{ self.class.affairs_of_state_config.column }=", status)
30
+ self.save
31
+ end
32
+ end
12
33
 
13
- validates(@_status_options[:column], inclusion: { in: @_statuses, allow_blank: @_status_options[:allow_blank] }, if: @_status_options[:if])
34
+ validates(affairs_of_state_config.column, inclusion: { in: affairs_of_state_config.statuses, allow_blank: affairs_of_state_config.allow_blank }, if: affairs_of_state_config.if)
14
35
 
15
- if @_status_options[:scopes]
16
- @_statuses.each do |status|
17
- raise ArgumentError.new("Affairs of State: '#{ status }' is not a valid status") unless valid_status?(status)
18
- self.scope status.to_sym, -> { where(@_status_options[:column] => status.to_s) }
36
+ if affairs_of_state_config.scopes
37
+ affairs_of_state_config.statuses.each do |status|
38
+ self.scope(status.to_sym, -> { where(affairs_of_state_config.column => status) })
19
39
  end
20
40
  end
21
41
 
@@ -25,41 +45,21 @@ module AffairsOfState
25
45
  true
26
46
  end
27
47
 
28
- private
29
-
30
- def valid_status?(status)
31
- ![:new].include?(status.to_sym)
48
+ def affairs_of_state_config
49
+ @affairs_of_state_config ||= AffairsOfState::Config.new
32
50
  end
33
51
  end
34
52
 
35
53
  module InstanceMethods
36
- def method_missing(method, *args)
37
- if self.class::STATUSES.map{ |s| "#{ s }?".to_sym }.include?(method)
38
- self.class.send(:define_method, method) do
39
- self.status == method.to_s.gsub(/\?$/, "")
40
- end
41
-
42
- send(method)
43
-
44
- elsif self.class::STATUSES.map{ |s| "#{ s }!".to_sym }.include?(method)
45
- self.class.send(:define_method, method) do
46
- self.send("#{ self.class.instance_variable_get('@_status_options')[:column] }=", method.to_s.gsub(/\!$/, ""))
47
- self.save
48
- end
49
-
50
- send(method)
51
- else
52
- super
53
- end
54
- end
55
54
  end
56
55
 
57
56
  module SingletonMethods
58
57
  def statuses_for_select
59
- @_statuses.map{ |s| [s.humanize, s] }
58
+ affairs_of_state_config.statuses.map{ |s| [s.humanize, s] }
60
59
  end
61
60
  end
62
-
63
61
  end
64
62
 
65
- ActiveRecord::Base.send :include, AffairsOfState
63
+ ActiveSupport.on_load(:active_record) do
64
+ ::ActiveRecord::Base.send :include, AffairsOfState
65
+ end
@@ -0,0 +1,16 @@
1
+ module AffairsOfState
2
+ class Config
3
+ attr_accessor :column, :allow_blank, :scopes, :if
4
+ attr_reader :statuses
5
+
6
+ def statuses=(val)
7
+ @statuses = val.flatten.map(&:to_s)
8
+
9
+ @statuses.each do |status|
10
+ raise ArgumentError.new("Affairs of State: '#{ status }' is not a valid status") if ["new"].include?(status)
11
+ end
12
+
13
+ @statuses
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module AffairsOfState
2
- VERSION = "0.2.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe AffairsOfState do
4
-
5
4
  describe "with a simple configuration" do
6
5
  class Pie < ActiveRecord::Base
7
6
  affairs_of_state :active, :inactive, :cancelled
@@ -71,6 +70,16 @@ describe AffairsOfState do
71
70
  it "should validate the column is set" do
72
71
  expect(Pie2.new(status: nil, super_status: "active")).to be_valid
73
72
  end
73
+
74
+ it "should know the accessors" do
75
+ expect(Pie2.new(status: nil, super_status: "inactive").inactive?).to be(true)
76
+ end
77
+
78
+ it "should know the setters" do
79
+ instance = Pie2.create!(status: nil, super_status: "inactive")
80
+ expect(instance.active!).to be(true)
81
+ expect(instance.super_status).to eq("active")
82
+ end
74
83
  end
75
84
 
76
85
  describe "without validations" do
@@ -167,4 +176,14 @@ describe AffairsOfState do
167
176
  end
168
177
  end
169
178
 
179
+ describe "multiple invocations" do
180
+ it "raises an error" do
181
+ expect(->{
182
+ class Pie9 < ActiveRecord::Base
183
+ affairs_of_state :not_important
184
+ affairs_of_state :something
185
+ end
186
+ }).to raise_error(ArgumentError, "Affairs of State: cannot be invoked multiple times on the same model")
187
+ end
188
+ end
170
189
  end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe AffairsOfState::Config do
4
+ subject{ AffairsOfState::Config.new }
5
+
6
+ describe "accessors" do
7
+ let(:expected){ double }
8
+
9
+ it "has :column" do
10
+ subject.column = expected
11
+ expect(subject.column).to eq(expected)
12
+ end
13
+
14
+ it "has :allow_blank" do
15
+ subject.allow_blank = expected
16
+ expect(subject.allow_blank).to eq(expected)
17
+ end
18
+
19
+ it "has :scopes" do
20
+ subject.scopes = expected
21
+ expect(subject.scopes).to eq(expected)
22
+ end
23
+
24
+ it "has :if" do
25
+ subject.if = expected
26
+ expect(subject.if).to eq(expected)
27
+ end
28
+ end
29
+
30
+ describe "#statuses=" do
31
+ it "converts to string" do
32
+ subject.statuses = [:a, :b]
33
+ expect(subject.statuses).to eq(["a", "b"])
34
+ end
35
+
36
+ it "flattens" do
37
+ subject.statuses = ["a", [:b]]
38
+ expect(subject.statuses).to eq(["a", "b"])
39
+ end
40
+
41
+ it "makes sure no invalid statuses are allowed" do
42
+ expect(->{
43
+ subject.statuses = [:new]
44
+ }).to raise_error(ArgumentError, "Affairs of State: 'new' is not a valid status")
45
+ end
46
+ end
47
+ end
@@ -1,7 +1,6 @@
1
- require 'active_record'
2
- require 'active_support/all'
3
- require 'affairs_of_state'
4
- require 'pry'
1
+ require "affairs_of_state"
2
+
3
+ require "pry"
5
4
 
6
5
  RSpec.configure do |config|
7
6
  config.run_all_when_everything_filtered = true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: affairs_of_state
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McPhillips
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-05 00:00:00.000000000 Z
11
+ date: 2020-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: Add a simple state to a gem, without all the hassle of a complex state
84
98
  machine.
85
99
  email:
@@ -101,8 +115,10 @@ files:
101
115
  - gemfiles/activerecord42.gemfile
102
116
  - gemfiles/activerecord50.gemfile
103
117
  - lib/affairs_of_state.rb
118
+ - lib/affairs_of_state/config.rb
104
119
  - lib/affairs_of_state/version.rb
105
120
  - spec/affairs_of_state_spec.rb
121
+ - spec/config_spec.rb
106
122
  - spec/db/.gitkeep
107
123
  - spec/spec_helper.rb
108
124
  homepage: http://github.com/kmcphillips/affairs_of_state
@@ -124,14 +140,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
140
  - !ruby/object:Gem::Version
125
141
  version: '0'
126
142
  requirements: []
127
- rubyforge_project:
128
- rubygems_version: 2.4.5.1
143
+ rubygems_version: 3.0.4
129
144
  signing_key:
130
145
  specification_version: 4
131
146
  summary: You have an Active Record model. It nees to have multiple states, but not
132
147
  complex rules. This gem gives you validation, easy check and change methods, and
133
148
  a single configuration line.
134
- test_files:
135
- - spec/affairs_of_state_spec.rb
136
- - spec/db/.gitkeep
137
- - spec/spec_helper.rb
149
+ test_files: []