status-manager 0.7.0 → 0.8.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 CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 63d931d22475e23678c82c510f8f704e8d1dbd06
4
- data.tar.gz: 0920aa2e322e0f6a28b927cf347bf97fb33e0fd9
5
- SHA512:
6
- metadata.gz: 94db437599edf8e5cb75c92f54569256229840e53f4ceccdb0bbb37b8baf137d397be7c673f4949ff67ff63162e4c7780419b2dc83f82c7b6ff2b7c3f2e18282
7
- data.tar.gz: ae62746a25cffa1a24c52f6dfc3833a85c4661c2a75cfc10eaaf8a21eddc63652c932956e92b572a9b3d8f0febb6d16e6626c0a5055251b5aba629d27b1cb584
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YWJlN2RkZjA2NmY0OTdiNWIyYWJmM2YxNzgwNGVhYjg3MDI5NTVkZQ==
5
+ data.tar.gz: !binary |-
6
+ MDg5ZWNkZDZlMWM0ZWE2MmZjNTM0OTAzNjcwNTEzMmJmY2U2MmFlMw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NDNjNzdhNzNjOTk0NTY2N2ZkZmNlMWY1NjA4MzNjMTUyMzBkZWM0MDkyNDQ3
10
+ NzAwYWQyNzdhYzllODVmMjhlZmYyYjM4YTNjY2NjMmYwZDE2ZWJiZDRmZjBj
11
+ NTZiYTY1MmMxYTczOTQzZmNiNDM4YWQ3OTg4MmMxMjA1YjBjNGQ=
12
+ data.tar.gz: !binary |-
13
+ NWI5ZWJlMmEzNGQ1NjQzZTNjZjY5NzBhMzA2MGJmMmY3OTQyMTg4ZTc2YmRk
14
+ MTNiNWEwMGI4ZTU2MTM1NzMzYmEwMDM1OTA3NmI0NmYxNjQzYTY4MDYyZjRm
15
+ ZTNkYWY5MjE4ZjU0Mzg4MWRmMWY3YzIwMmM0MDllZDNiNWNlNTg=
data/README.md CHANGED
@@ -1,6 +1,3 @@
1
- status-manager
2
- ==============
3
-
4
1
  ## Description
5
2
  ActiveRecord Model Status Manager, It provides easy ways managing model that have many statuses.
6
3
 
@@ -21,8 +18,7 @@ class Product < ActiveRecord::Base
21
18
 
22
19
  # attr_as_status :status_attribute in model, {:status_value => 'status_value that is saved in database'}
23
20
  attr_as_status :sale_status, {:onsale => 'onsale', :reject => 'reject', :pending => 'pending', :soldout => 'soldout'}
24
- status_group :sale_status, {:close => [:reject, :pending], :open => [:onsale, :soldout]}
25
-
21
+ status_group :sale_status, {:close => [:reject, :pending], :open => [:onsale, :soldout]}
26
22
  end
27
23
  ```
28
24
 
@@ -32,17 +28,22 @@ end
32
28
  Product.sale_statuses #=> {:onsale => 'onsale', :reject => 'reject', :pending => 'pending', :soldout => 'soldout'}
33
29
 
34
30
  ## use scope
35
- @onsale_product = Product.sale_status_onsale.first # or Product.sale_status(:onsale).first
31
+ @onsale_product = Product.sale_status_onsale.first
36
32
  @closed_product = Product.sale_status_close.first
33
+ #or using symbol
34
+ Product.sale_status(:onsale)
35
+ #or multiple statuses (with group statuses)
36
+ Product.sale_status(:onsale, :pending)
37
37
 
38
38
  @onsale_product.sale_status_onsale? #=> true
39
39
  #or
40
40
  @closed_product.sale_status?(:close) #=> true
41
41
 
42
+
42
43
  ## change attribute value
43
44
  @closed_product.sale_status_to(:onsale)
44
45
  #or
45
- @closed_product.sale_Status_to_onsale
46
+ @closed_product.sale_status_to_onsale
46
47
 
47
48
  ## update value with database
48
49
  @closed_product.update_sale_status_to(:onsale)
@@ -50,6 +51,16 @@ Product.sale_statuses #=> {:onsale => 'onsale', :reject => 'reject', :pending =>
50
51
  @closed_product.update_sale_status_to_onsale
51
52
  ```
52
53
 
54
+ #### Inspect status changing
55
+ ```ruby
56
+ pending_product = Product.sale_status(:pending).first
57
+ pending_product.sale_status_to(:onsale)
58
+ pending_product.sale_status_changed? #=> true
59
+ pending_product.sale_status_changed?(:from => :pending, :to => :onsale) #=> true
60
+ pending_product.sale_Status_changed?(:to => onsale) #=>true
61
+ pending_product.sale_Status_changed?(:from => onsale) #=> true
62
+ ```
63
+
53
64
  #### Callback
54
65
  ``` ruby
55
66
  class Product < ActiveRecord::Base
@@ -61,20 +72,22 @@ class Product < ActiveRecord::Base
61
72
 
62
73
  #callback update status from specific status
63
74
  before_status_update :sale_status, :onsale => :close do |product|
64
- puts "#{product.title} is closed"
75
+ product.sale_status_changed?(:from => :onsale, :to => :close) #=> true
76
+ # do somthing
65
77
  end
66
78
 
67
79
  after_status_update :sale_status, :close => :onsale do |product|
68
- puts "closed #{product.title} is opened"
69
80
  # do something after update
70
81
  end
71
82
 
72
83
  #callback update status
73
84
  after_status_update :sale_status, :reject do |prdocut|
74
- puts "#{product.title} is rejected"
85
+ product.sale_status_changed? #=> true
86
+ product.sale_status?(:reject) #=> true
75
87
  # do something after update
76
88
  end
77
89
  end
78
90
 
79
91
  ```
80
92
 
93
+ www.myrealtrip.com uses status-manager
@@ -18,13 +18,7 @@ module StatusManager
18
18
  status_store = StatusStore.new(status_attribute, status_sets)
19
19
  status_store_list.add(status_store)
20
20
 
21
- scope "#{status_store.attribute_name}", lambda{ | status |
22
- if status_store.status?(status)
23
- where("#{self.table_name}.#{status_store.attribute_name.to_s}" => status_store_list.get(status_store.attribute_name).value(status))
24
- elsif status_store.group_status?(status)
25
- where("#{self.table_name}.#{status_store.attribute_name} in (?)", status_store.get_group_status_sets(status).values)
26
- end
27
- }
21
+ scope "#{status_store.attribute_name}", lambda{ | statuses | where("#{self.table_name}.#{status_store.attribute_name.to_s}" => status_store.values(statuses)) }
28
22
 
29
23
  status_store.status_sets.each do |key, value|
30
24
  #active_record scope setting
@@ -60,18 +54,39 @@ module StatusManager
60
54
 
61
55
  #status setter (do not override attr_accessible)
62
56
  define_method "#{status_store.attribute_name}_to" do |status|
57
+ raise "#{status} is undefined status or it is group status" unless status_store.status?(status)
63
58
  status_value = self.class.status_store_list.get(status_store.attribute_name).value(status)
64
59
  self.send("#{status_store.attribute_name}=", status_value)
65
60
  end
66
61
 
67
62
  # update status
68
63
  define_method "update_#{status_store.attribute_name}_to" do |status|
64
+ raise "#{status} is undefined status or it is group status" unless status_store.status?(status)
69
65
  self.update_attributes(status_attribute.to_sym => self.class.status_store_list.get(status_store.attribute_name).value(status))
70
66
  end
71
67
 
68
+ define_method("#{status_store.attribute_name}_changed?") do |options={}|
69
+ statuses = self.send("#{status_store.attribute_name}_change")
70
+ if statuses
71
+ if statuses[0] == statuses[1]
72
+ return false
73
+ elsif options[:from] && options[:to]
74
+ self.send("#{status_store.attribute_name}_was?", options[:from]) && self.send("#{status_store.attribute_name}?", options[:to])
75
+ elsif options[:to]
76
+ self.send("#{status_store.attribute_name}?", options[:to])
77
+ elsif options[:from]
78
+ self.send("#{status_store.attribute_name}_was?", options[:from])
79
+ else
80
+ return true
81
+ end
82
+ else
83
+ return false
84
+ end
85
+ end
86
+
72
87
  #get status list
73
88
  define_singleton_method "#{status_store.attribute_name.to_s.pluralize}" do
74
- self.class.status_store_list.get(status_attribute).status_sets
89
+ self.status_store_list.get(status_store.attribute_name).status_sets
75
90
  end
76
91
  end
77
92
 
@@ -82,8 +97,6 @@ module StatusManager
82
97
  self.class_variable_set(:@@status_store_list, StatusStoreList.new)
83
98
  end
84
99
  end
85
-
86
-
87
100
  end
88
101
  end
89
102
 
@@ -9,15 +9,15 @@ module StatusManager
9
9
  status_store.add_group_status(group_status_name, group_statuses)
10
10
 
11
11
  # set scope
12
- scope "#{status_store.attribute_name}_#{group_status_name}", where("#{self.table_name}.#{status_store.attribute_name} in (?)", status_store.get_group_status_sets(group_status_name).values)
12
+ scope "#{status_store.attribute_name}_#{group_status_name}", where("#{self.table_name}.#{status_store.attribute_name} in (?)", status_store.get_group_status(group_status_name).values)
13
13
 
14
14
  # status check method
15
15
  define_method "#{status_attribute_name}_#{group_status_name}?" do
16
- status_store.get_group_status_sets(group_status_name).values.include? self.send(status_attribute_name)
16
+ status_store.get_group_status(group_status_name).values.include? self.send(status_attribute_name)
17
17
  end
18
18
 
19
19
  define_method "#{status_attribute_name}_was_#{group_status_name}?" do
20
- status_store.get_group_status_sets(group_status_name).values.include? self.send("#{status_attribute_name}_was")
20
+ status_store.get_group_status(group_status_name).values.include? self.send("#{status_attribute_name}_was")
21
21
  end
22
22
  end
23
23
  end
@@ -24,35 +24,50 @@ module StatusManager
24
24
  @group_statuses = {}
25
25
  end
26
26
 
27
- def status?(status)
28
- @status_sets.key?(status)
29
- end
30
-
31
- def group_status?(group_status)
32
- @group_statuses.key?(group_status)
33
- end
34
-
35
27
  def statuses
36
28
  @status_sets.keys
37
29
  end
38
30
 
39
- def values
40
- @status_sets.values
31
+ def status?(status)
32
+ @status_sets.key?(status)
41
33
  end
42
34
 
43
35
  def value(status)
44
36
  @status_sets[status]
45
37
  end
46
38
 
47
- def status(value)
48
- @status_sets.select{ |status, _value| _value == value}.first[0]
39
+ def group_status?(group_status)
40
+ @group_statuses.key?(group_status)
41
+ end
42
+
43
+ #scope에 array 파라미터 넣기 적용
44
+ def values(statuses=[])
45
+ if statuses.nil?
46
+ return @statuse_sets.values
47
+ elsif status? statuses
48
+ return [value(statuses)]
49
+ elsif group_status? statuses
50
+ return get_group_status(statuses).values
51
+ elsif statuses.instance_of?(Array)
52
+ results = []
53
+ statuses.each do |_status|
54
+ if status?(_status)
55
+ results << value(_status)
56
+ elsif group_status?(_status)
57
+ results |= get_group_status(_status).values
58
+ end
59
+ end
60
+ return results.uniq
61
+ else
62
+ return []
63
+ end
49
64
  end
50
65
 
51
66
  def add_group_status(group_status_name, statuses)
52
67
  @group_statuses.merge!({group_status_name => statuses})
53
68
  end
54
69
 
55
- def get_group_status_sets(group_status_name)
70
+ def get_group_status(group_status_name)
56
71
  statuses = {}
57
72
  @group_statuses[group_status_name].each do |status|
58
73
  statuses[status] = self.value(status)
@@ -1,27 +1,25 @@
1
1
  module StatusManager
2
2
  module StatusUpdateCallback
3
- def after_status_update(status_title, status_way, &block)
3
+ def after_status_update(attribute_name, status_way, &block)
4
4
  self.after_update do |obj|
5
- self.class.send(:status_update_callback, obj, status_title, status_way, &block)
5
+ self.class.send(:status_update_callback, obj, attribute_name, status_way, &block)
6
6
  end
7
7
  end
8
8
 
9
- def before_status_update(status_title, status_way, &block)
9
+ def before_status_update(attribute_name, status_way, &block)
10
10
  self.before_update do |obj|
11
- self.class.send(:status_update_callback, obj, status_title, status_way, &block)
11
+ self.class.send(:status_update_callback, obj, attribute_name, status_way, &block)
12
12
  end
13
13
  end
14
14
 
15
- def status_update_callback(obj, status_title, status_way, &block)
16
- if obj.send("#{status_title}_changed?")
17
- if status_way.class == Hash
18
- prev_status = status_way.first[0]
19
- next_status = status_way.first[1]
20
- if obj.send("#{status_title}_was_#{prev_status}?") && obj.send("#{status_title}_#{next_status}?")
15
+ def status_update_callback(obj, attribute_name, status_way, &block)
16
+ if obj.send("#{attribute_name}_changed?")
17
+ if status_way.instance_of?(Hash)
18
+ if obj.send("#{attribute_name}_changed?", {:from => status_way.first[0], :to => status_way.first[1]})
21
19
  block.call(obj)
22
20
  end
23
21
  elsif status_way.class == Symbol
24
- if obj.send("#{status_title}_#{status_way}?")
22
+ if obj.send("#{attribute_name}_#{status_way}?")
25
23
  block.call(obj)
26
24
  end
27
25
  end
Binary file
@@ -1,9 +1,9 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'status-manager'
3
- s.version = '0.7.0'
4
- s.date = '2013-12-20'
3
+ s.version = '0.8.1'
4
+ s.date = '2013-12-21'
5
5
  s.summary = "ActiveRecord Model Status Manager"
6
- s.description = "ActiveRecord Model Status Manager, It provides easy ways managing model that have many statuses."
6
+ s.description = "ActiveRecord Model Status Manager, It provides easy ways for managing ActiveModels that have many statuses."
7
7
  s.authors = ["Keepcosmos"]
8
8
  s.email = 'keepcosmos@gmail.com'
9
9
  s.licenses = 'MIT'
Binary file
@@ -3,10 +3,13 @@ class Product < ActiveRecord::Base
3
3
 
4
4
  # acts_as_status :status_attribute in model, {:status_value => 'status_value that is saved in database'}
5
5
  attr_as_status :sale_status, :onsale => 'onsale', :reject => 'reject', :pending => 'pending', :soldout => 'soldout'
6
- status_group :sale_status, :close => [:reject, :pending], :open => [:onsale, :soldout]
6
+ status_group :sale_status, {
7
+ :close => [:reject, :pending],
8
+ :display => [:onsale, :soldout]
9
+ }
7
10
 
8
11
  before_status_update :sale_status, :close => :onsale do |product|
9
- puts "open #{product.title}"
12
+ puts "display #{product.title}"
10
13
  end
11
14
 
12
15
  after_status_update :sale_status, :pending => :onsale do |product|
@@ -17,4 +20,8 @@ class Product < ActiveRecord::Base
17
20
  puts "onsale #{product.title}"
18
21
  end
19
22
 
23
+ after_status_update :sale_status, :display => :close do |product|
24
+ puts "close #{product.title}"
25
+ end
26
+
20
27
  end
@@ -3,13 +3,30 @@ require 'models/product'
3
3
 
4
4
  class StatusManagerTest < Test::Unit::TestCase
5
5
 
6
- def test_status_change
6
+ def test_change
7
+ product = Product.sale_status(:onsale).first
8
+ assert !product.sale_status_changed?
9
+ product.sale_status_to(:pending)
10
+ assert product.sale_status_changed?
11
+ assert product.sale_status_changed?(:from => :display, :to => :close)
12
+ assert product.sale_status_changed?(:from => :onsale)
13
+ assert product.sale_status_changed?(:to => :close)
14
+ product.save
15
+ assert !product.sale_status_changed?
16
+ end
17
+
18
+ def test_status_update
7
19
  product = Product.sale_status(:pending).first
8
20
  assert product.sale_status?(:pending)
9
21
  product.update_sale_status_to_onsale
10
22
  assert product.sale_status?(:onsale)
11
23
  end
12
24
 
25
+ def test_multiple_status_scope
26
+ assert Product.sale_status(Product.sale_statuses.keys).size == Product.all.size
27
+ assert Product.sale_status([:display, :reject]).size == (Product.all.size - Product.sale_status(:pending).size)
28
+ end
29
+
13
30
  def test_current_status
14
31
  products = Product.sale_status_onsale
15
32
  products.each do |product|
@@ -40,17 +57,17 @@ class StatusManagerTest < Test::Unit::TestCase
40
57
 
41
58
  def test_group_status
42
59
  closed_products = Product.sale_status_close
43
- closed_products = Product.sale_status(:close)
44
- assert closed_products == _closed_products
60
+ _closed_products = Product.sale_status(:close)
61
+ assert closed_products.size == _closed_products.size
45
62
  closed_products.each do |product|
46
63
  assert product.sale_status_close?
47
64
  assert product.sale_status? :close
48
65
  end
49
66
 
50
- opened_products = Product.sale_status_open
51
- opened_products.each do |product|
52
- assert product.sale_status_open?
53
- assert product.sale_status? :open
67
+ displayed_products = Product.sale_status_display
68
+ displayed_products.each do |product|
69
+ assert product.sale_status_display?
70
+ assert product.sale_status? :display
54
71
  end
55
72
  end
56
73
 
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: status-manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keepcosmos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-20 00:00:00.000000000 Z
11
+ date: 2013-12-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: ActiveRecord Model Status Manager, It provides easy ways managing model
14
- that have many statuses.
13
+ description: ActiveRecord Model Status Manager, It provides easy ways for managing
14
+ ActiveModels that have many statuses.
15
15
  email: keepcosmos@gmail.com
16
16
  executables: []
17
17
  extensions: []
@@ -26,6 +26,7 @@ files:
26
26
  - lib/status-manager/status_validation.rb
27
27
  - lib/status-manager/version.rb
28
28
  - status-manager-0.1.3.gem
29
+ - status-manager-0.7.0.gem
29
30
  - status-manager.gemspec
30
31
  - status_manager_test.sqlite3
31
32
  - test/config/database.yml
@@ -44,17 +45,17 @@ require_paths:
44
45
  - lib
45
46
  required_ruby_version: !ruby/object:Gem::Requirement
46
47
  requirements:
47
- - - '>='
48
+ - - ! '>='
48
49
  - !ruby/object:Gem::Version
49
50
  version: '0'
50
51
  required_rubygems_version: !ruby/object:Gem::Requirement
51
52
  requirements:
52
- - - '>='
53
+ - - ! '>='
53
54
  - !ruby/object:Gem::Version
54
55
  version: '0'
55
56
  requirements: []
56
57
  rubyforge_project:
57
- rubygems_version: 2.1.2
58
+ rubygems_version: 2.0.7
58
59
  signing_key:
59
60
  specification_version: 4
60
61
  summary: ActiveRecord Model Status Manager