kentouzu 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## v0.0.9
2
+
3
+ * Added `all_with_reified_drafts` method.
4
+ * Changed `()` to `call` for clarity in overridden `save` method.
5
+ * Tidied up some formatting.
6
+
1
7
  ## v0.0.8
2
8
 
3
9
  * Fixed bug that occurs when reifying object with STI when type column is blank.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kentouzu (0.0.8)
4
+ kentouzu (0.0.9)
5
5
  activerecord (>= 3.0)
6
6
  railties (>= 3.0)
7
7
 
data/README.md CHANGED
@@ -14,11 +14,11 @@ Add the gem to your project's Gemfile:
14
14
 
15
15
  Generate a migration for the drafts table:
16
16
 
17
- rails g kentouzu:install
17
+ $ rails g kentouzu:install
18
18
 
19
19
  Run the migration:
20
20
 
21
- rake db:migrate
21
+ $ rake db:migrate
22
22
 
23
23
  Add `has_drafts` to the models you want to have drafts on.
24
24
 
@@ -7,7 +7,7 @@ class Draft < ActiveRecord::Base
7
7
 
8
8
  validates_presence_of :event
9
9
 
10
- def self.with_item_keys item_type, item_id
10
+ def self.with_item_keys(item_type, item_id)
11
11
  scoped :conditions => { :item_type => item_type, :item_id => item_id }
12
12
  end
13
13
 
@@ -27,7 +27,7 @@ class Draft < ActiveRecord::Base
27
27
 
28
28
  scope :between, lambda { |start_time, end_time| where(["#{Kentouzu.timestamp_field} > ? AND #{Kentouzu.timestamp_field} < ?", start_time, end_time]).order("#{Kentouzu.timestamp_field} ASC, #{self.primary_key} ASC") }
29
29
 
30
- def reify options = {}
30
+ def reify(options = {})
31
31
  without_identity_map do
32
32
  options[:has_one] = 3 if options[:has_one] == true
33
33
  options.reverse_merge! :has_one => false
@@ -95,7 +95,7 @@ class Draft < ActiveRecord::Base
95
95
 
96
96
  private
97
97
 
98
- def without_identity_map &block
98
+ def without_identity_map(&block)
99
99
  if defined?(ActiveRecord::IdentityMap) && ActiveRecord::IdentityMap.respond_to?(:without)
100
100
  ActiveRecord::IdentityMap.without &block
101
101
  else
@@ -103,7 +103,7 @@ class Draft < ActiveRecord::Base
103
103
  end
104
104
  end
105
105
 
106
- def reify_has_ones model, lookback
106
+ def reify_has_ones(model, lookback)
107
107
  model.class.reflect_on_all_associations(:has_one).each do |association|
108
108
  child = model.send association.name
109
109
 
@@ -5,7 +5,7 @@ module Kentouzu
5
5
  end
6
6
 
7
7
  module ClassMethods
8
- def has_drafts options = {}
8
+ def has_drafts(options = {})
9
9
  send :include, InstanceMethods
10
10
 
11
11
  class_attribute :draft_association_name
@@ -47,6 +47,32 @@ module Kentouzu
47
47
  Draft.where(:item_type => self.name, :event => 'create')
48
48
  end
49
49
 
50
+ define_singleton_method "all_with_reified_#{drafts_association_name.to_s}".to_sym do |order_by = Kentouzu.timestamp_field|
51
+ existing_drafts = Draft.where("`drafts`.`item_type` = \"#{self.name}\" AND `drafts`.`item_id` IS NOT NULL").group_by { |draft| draft.item_id }.map { |k, v| v.sort_by { |draft| draft.created_at }.first }
52
+
53
+ new_drafts = Draft.where("`drafts`.`item_type` = \"#{self.name}\" AND `drafts`.`item_id` IS NULL")
54
+
55
+ existing_reified_objects = existing_drafts.map { |draft| draft.reify }
56
+
57
+ new_reified_objects = new_drafts.map do |draft|
58
+ object = draft.reify
59
+
60
+ object.send "#{Kentouzu.timestamp_field}=", draft.created_at
61
+
62
+ object
63
+ end
64
+
65
+ existing_objects = self.all.reject { |object| existing_reified_objects.map { |reified_object| reified_object.id }.include? object.id }
66
+
67
+ all_objects = (existing_objects + existing_reified_objects + new_reified_objects).sort_by { |object| object.send order_by }
68
+
69
+ if block_given?
70
+ all_objects.select! { |object| yield object }
71
+ end
72
+
73
+ all_objects
74
+ end
75
+
50
76
  def drafts_off
51
77
  self.drafts_enabled_for_model = false
52
78
  end
@@ -67,7 +93,7 @@ module Kentouzu
67
93
 
68
94
  draft.save
69
95
  else
70
- default_save.bind(self).()
96
+ default_save.bind(self).call
71
97
  end
72
98
  end
73
99
  end
@@ -76,12 +102,13 @@ module Kentouzu
76
102
  source_draft.nil?
77
103
  end
78
104
 
79
- def draft_at timestamp, reify_options = {}
105
+ def draft_at(timestamp, reify_options = {})
80
106
  v = send(self.class.versions_association_name).following(timestamp).first
107
+
81
108
  v ? v.reify(reify_options) : self
82
109
  end
83
110
 
84
- def with_drafts method = nil
111
+ def with_drafts(method = nil)
85
112
  drafts_were_enabled = self.drafts_enabled_for_model
86
113
 
87
114
  self.class.drafts_on
@@ -91,7 +118,7 @@ module Kentouzu
91
118
  self.class.drafts_off unless drafts_were_enabled
92
119
  end
93
120
 
94
- def without_drafts method = nil
121
+ def without_drafts(method = nil)
95
122
  drafts_were_enabled = self.drafts_enabled_for_model
96
123
 
97
124
  self.class.drafts_off
@@ -1,3 +1,3 @@
1
1
  module Kentouzu
2
- VERSION = '0.0.8'
2
+ VERSION = '0.0.9'
3
3
  end
data/lib/kentouzu.rb CHANGED
@@ -7,7 +7,7 @@ require 'kentouzu/has_drafts'
7
7
  require 'kentouzu/draft'
8
8
 
9
9
  module Kentouzu
10
- def self.enabled= value
10
+ def self.enabled=(value)
11
11
  Kentouzu.config.enabled = value
12
12
  end
13
13
 
@@ -15,7 +15,7 @@ module Kentouzu
15
15
  !!Kentouzu.config.enabled
16
16
  end
17
17
 
18
- def self.enabled_for_controller= value
18
+ def self.enabled_for_controller=(value)
19
19
  drafts_store[:request_enabled_for_controller] = value
20
20
  end
21
21
 
@@ -23,7 +23,7 @@ module Kentouzu
23
23
  !!drafts_store[:request_enabled_for_controller]
24
24
  end
25
25
 
26
- def self.timestamp_field= field_name
26
+ def self.timestamp_field=(field_name)
27
27
  Kentouzu.config.timestamp_field = field_name
28
28
  end
29
29
 
@@ -31,7 +31,7 @@ module Kentouzu
31
31
  Kentouzu.config.timestamp_field
32
32
  end
33
33
 
34
- def self.source= value
34
+ def self.source=(value)
35
35
  drafts_store[:source] = value
36
36
  end
37
37
 
@@ -39,7 +39,7 @@ module Kentouzu
39
39
  drafts_store[:source]
40
40
  end
41
41
 
42
- def self.controller_info= value
42
+ def self.controller_info=(value)
43
43
  drafts_store[:controller_info] = value
44
44
  end
45
45
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kentouzu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-13 00:00:00.000000000 Z
12
+ date: 2013-08-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
@@ -259,7 +259,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
259
259
  version: '0'
260
260
  segments:
261
261
  - 0
262
- hash: 3721753059103140299
262
+ hash: 1950418574329021728
263
263
  required_rubygems_version: !ruby/object:Gem::Requirement
264
264
  none: false
265
265
  requirements:
@@ -268,7 +268,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
268
268
  version: '0'
269
269
  segments:
270
270
  - 0
271
- hash: 3721753059103140299
271
+ hash: 1950418574329021728
272
272
  requirements: []
273
273
  rubyforge_project: kentouzu
274
274
  rubygems_version: 1.8.25