affairs_of_state 0.2.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/README.md +2 -0
- data/affairs_of_state.gemspec +1 -0
- data/lib/affairs_of_state.rb +35 -35
- data/lib/affairs_of_state/config.rb +16 -0
- data/lib/affairs_of_state/version.rb +1 -1
- data/spec/affairs_of_state_spec.rb +20 -1
- data/spec/config_spec.rb +47 -0
- data/spec/spec_helper.rb +3 -4
- metadata +20 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e6950061957896a9e0bf23635b6b97d75c97fd09bf52eb51fa1fb30352842a00
|
|
4
|
+
data.tar.gz: 8521df3d7183885b795982184e07adb4492a085f2fc46d0457af8f61f16d8f6b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 637f0bd6245bfc644145f0640fd690231a729b6a6557c034ccf0b8a07582e388335f6e49c9637428c739c4edbf15ccf19d867bb9a45ca87f0fbe5a5412e7f31f
|
|
7
|
+
data.tar.gz: 9cfb5bff07dea9c39183c89f4469df774dc1470997707eff32b26d725bffa35a7e129dbf72369ef7557b44f0d55b805fce7fe9c8dcec44e5094fc2b88e6ed7e3
|
data/README.md
CHANGED
data/affairs_of_state.gemspec
CHANGED
data/lib/affairs_of_state.rb
CHANGED
|
@@ -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(*
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
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
|
-
|
|
58
|
+
affairs_of_state_config.statuses.map{ |s| [s.humanize, s] }
|
|
60
59
|
end
|
|
61
60
|
end
|
|
62
|
-
|
|
63
61
|
end
|
|
64
62
|
|
|
65
|
-
|
|
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,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
|
data/spec/config_spec.rb
ADDED
|
@@ -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
|
data/spec/spec_helper.rb
CHANGED
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.
|
|
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:
|
|
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
|
-
|
|
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: []
|