Skip to content

Commit

Permalink
Wow Concern.
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Nov 20, 2014
1 parent b4ce45c commit ba8bf19
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions app/models/post/parent_methods.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
module Post::ParentMethods
extend ActiveSupport::Concern

included do
after_save :update_parent
validate :validate_parent
set_callback :delete, :after, :give_favorites_to_parent
versioned :parent_id, :default => nil
has_many :children, lambda { where("status <> ?", "deleted").order("id") },
:class_name => "Post", :foreign_key => :parent_id
end

module ClassMethods
def update_has_children(post_id)
has_children = Post.exists?(["parent_id = ? AND status <> 'deleted'", post_id])
Expand Down Expand Up @@ -28,16 +39,6 @@ def set_parent(post_id, parent_id, old_parent_id = nil)
end
end

def self.included(m)
m.extend(ClassMethods)
m.after_save :update_parent
m.validate :validate_parent
m.set_callback :delete, :after, :give_favorites_to_parent
m.versioned :parent_id, :default => nil
m.has_many :children, lambda { where("status <> ?", "deleted").order("id") },
:class_name => "Post", :foreign_key => :parent_id
end

def validate_parent
errors.add("parent_id") unless parent_id.nil? || Post.exists?(parent_id)
end
Expand Down

0 comments on commit ba8bf19

Please sign in to comment.