Module: Kentouzu::Model::ClassMethods
- Defined in:
- lib/kentouzu/has_drafts.rb
Instance Method Summary collapse
- #draft_class ⇒ Object
- #drafts_enabled_for_model? ⇒ Boolean
- #drafts_off ⇒ Object
- #drafts_off! ⇒ Object
- #drafts_on ⇒ Object
- #drafts_on! ⇒ Object
-
#has_drafts(options = {}) ⇒ Object
By calling this in your model all subsequent calls to save will instead create a draft.
Instance Method Details
#draft_class ⇒ Object
128 129 130 |
# File 'lib/kentouzu/has_drafts.rb', line 128 def draft_class @draft_class ||= draft_class_name.constantize end |
#drafts_enabled_for_model? ⇒ Boolean
124 125 126 |
# File 'lib/kentouzu/has_drafts.rb', line 124 def drafts_enabled_for_model? Kentouzu.enabled_for_model?(self) end |
#drafts_off ⇒ Object
108 109 110 111 112 |
# File 'lib/kentouzu/has_drafts.rb', line 108 def drafts_off warn 'DEPRECATED: use `drafts_off!` instead of `drafts_off`. Will be removed in Kentouzu 0.3.0.' self.drafts_off! end |
#drafts_off! ⇒ Object
104 105 106 |
# File 'lib/kentouzu/has_drafts.rb', line 104 def drafts_off! Kentouzu.enabled_for_model(self, false) end |
#drafts_on ⇒ Object
118 119 120 121 122 |
# File 'lib/kentouzu/has_drafts.rb', line 118 def drafts_on warn 'DEPRECATED: use `drafts_on!` instead of `drafts_on`. Will be removed in Kentouzu 0.3.0.' self.drafts_on! end |
#drafts_on! ⇒ Object
114 115 116 |
# File 'lib/kentouzu/has_drafts.rb', line 114 def drafts_on! Kentouzu.enabled_for_model(self, true) end |
#has_drafts(options = {}) ⇒ Object
By calling this in your model all subsequent calls to save will instead create a draft. Drafts are available through the ‘drafts` association.
Options: :class_name The name of a custom Draft class. Should inherit from ‘Kentouzu::Draft`.
Default is `'Draft'`.
:draft The name for the method which returns the draft the instance was reified from.
Default is `:draft`.
:drafts The name to use for the drafts association.
Default is `:drafts`.
:if Proc that allows you to specify the conditions under which drafts are made. :ignore An Array of attributes that will be ignored when creating a ‘Draft`.
Can also accept a Has as an argument where each key is the attribute to ignore (either
a `String` or `Symbol`) and each value is a `Proc` whose return value, `true` or
`false`, determines if it is ignored.
:meta A hash of extra data to store. Each key in the hash (either a ‘String` or `Symbol`)
must be a column on the `drafts` table, otherwise it is ignored. You must add these
columns yourself. The values are either objects or procs (which are called with `self`,
i.e. the model the draft is being made from).
:on An array of events that will cause a draft to be created.
Defaults to `[:create, :update, :destroy]`.
:only Inverse of the ‘:ignore` option. Only the attributes supplied will be passed along to
the draft.
:unless Proc that allows you to specify the conditions under which drafts are not made.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/kentouzu/has_drafts.rb', line 32 def has_drafts( = {}) # Only include the instance methods when this `has_drafts` is called to avoid cluttering up models. send :include, InstanceMethods # Add `before_draft_save`, `after_draft_save`, and `around_draft_save` callbacks. send :define_model_callbacks, :draft_save class_attribute :draft_association_name self.draft_association_name = [:draft] || :draft # The draft this instance was reified from. attr_accessor self.draft_association_name class_attribute :draft_class_name self.draft_class_name = [:class_name] || 'Draft' class_attribute :draft_options self. = .dup [:ignore, :only].each do |option| [option] = [[option]].flatten.compact.map { |attr| attr.is_a?(Hash) ? attr.stringify_keys : attr.to_s } end [:meta] ||= {} class_attribute :drafts_association_name self.drafts_association_name = [:drafts] || :drafts if ActiveRecord::VERSION::MAJOR >= 4 # `has_many` syntax for specifying order uses a lambda in Rails 4 has_many self.drafts_association_name, lambda { order("#{Kentouzu.} ASC, #{self.primary_key} ASC") }, :class_name => draft_class_name, :as => :item, :dependent => :destroy else has_many self.drafts_association_name, :class_name => draft_class_name, :as => :item, :order => "#{Kentouzu.} ASC, #{self.draft_class.primary_key} ASC", :dependent => :destroy end define_singleton_method "new_#{drafts_association_name.to_s}".to_sym do Draft.where(:item_type => self.name, :event => 'create') end define_singleton_method "all_with_reified_#{drafts_association_name.to_s}".to_sym do |order_by = Kentouzu., &block| existing_drafts = Draft.where("`drafts`.`item_type` = \"#{self.base_class.name}\" AND `drafts`.`item_id` IS NOT NULL").group_by { |draft| draft.item_id }.map { |_, v| v.sort_by { |draft| draft.created_at }.last } new_drafts = Draft.where("`drafts`.`item_type` = \"#{self.base_class.name}\" AND `drafts`.`item_id` IS NULL") existing_reified_objects = existing_drafts.map { |draft| draft.reify } new_reified_objects = new_drafts.map do |draft| object = draft.reify object.send "#{Kentouzu.}=", draft.created_at object end existing_objects = self.all.reject { |object| existing_reified_objects.map { |reified_object| reified_object.id }.include? object.id } all_objects = (existing_objects + existing_reified_objects + new_reified_objects).sort_by { |object| object.send order_by } if block all_objects.select! { |object| block.call(object) } end all_objects end def drafts_off! Kentouzu.enabled_for_model(self, false) end def drafts_off warn 'DEPRECATED: use `drafts_off!` instead of `drafts_off`. Will be removed in Kentouzu 0.3.0.' self.drafts_off! end def drafts_on! Kentouzu.enabled_for_model(self, true) end def drafts_on warn 'DEPRECATED: use `drafts_on!` instead of `drafts_on`. Will be removed in Kentouzu 0.3.0.' self.drafts_on! end def drafts_enabled_for_model? Kentouzu.enabled_for_model?(self) end def draft_class @draft_class ||= draft_class_name.constantize end end |