Module: Kentouzu::Model::InstanceMethods
- Defined in:
- lib/kentouzu/has_drafts.rb
Class Method Summary collapse
-
.included(base) ⇒ Object
Override the default ‘save` method and replace it with one that checks to see if a draft should be saved.
Instance Method Summary collapse
- #draft_at(timestamp, reify_options = {}) ⇒ Object
- #drafts_enabled_for_model? ⇒ Boolean
- #live? ⇒ Boolean
- #with_drafts(method = nil) ⇒ Object
- #without_drafts(method = nil) ⇒ Object
Class Method Details
.included(base) ⇒ Object
Override the default ‘save` method and replace it with one that checks to see if a draft should be saved. If a draft should be saved the original object instance is left untouched and a new draft is created.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/kentouzu/has_drafts.rb', line 137 def self.included(base) default_save = base.instance_method(:save) base.send :define_method, :save do |*| if switched_on? && save_draft? save_draft else default_save.bind(self).call end end default_save_with_bang = base.instance_method(:save!) base.send :define_method, :save! do |*| if switched_on? && save_draft? save_draft else default_save_with_bang.bind(self).call end end end |
Instance Method Details
#draft_at(timestamp, reify_options = {}) ⇒ Object
163 164 165 166 167 |
# File 'lib/kentouzu/has_drafts.rb', line 163 def draft_at(, = {}) v = send(self.class.versions_association_name).following().first v ? v.reify() : self end |
#drafts_enabled_for_model? ⇒ Boolean
189 190 191 |
# File 'lib/kentouzu/has_drafts.rb', line 189 def drafts_enabled_for_model? self.class.drafts_enabled_for_model? end |
#live? ⇒ Boolean
159 160 161 |
# File 'lib/kentouzu/has_drafts.rb', line 159 def live? source_draft.nil? end |
#with_drafts(method = nil) ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'lib/kentouzu/has_drafts.rb', line 169 def with_drafts(method = nil) drafts_were_enabled = self.drafts_enabled_for_model self.class.drafts_on method ? method.to_proc.call(self) : yield ensure self.class.drafts_off unless drafts_were_enabled end |
#without_drafts(method = nil) ⇒ Object
179 180 181 182 183 184 185 186 187 |
# File 'lib/kentouzu/has_drafts.rb', line 179 def without_drafts(method = nil) drafts_were_enabled = self.drafts_enabled_for_model self.class.drafts_off method ? method.to_proc.call(self) : yield ensure self.class.drafts_on if drafts_were_enabled end |