Sha256: 3f2c235351ca99996208f8b6d7ca672978ed636ae758b4f68c0f44efa8ab83dc

Contents?: true

Size: 1.34 KB

Versions: 6

Compression:

Stored size: 1.34 KB

Contents

module TheComments
  module User
    extend ActiveSupport::Concern
    
    included do
      has_many :comcoms, class_name: :Comment, foreign_key: :holder_id
    end

    def my_comments; ::Comment.where(user: self); end

    %w[draft published deleted].each do |state|
      define_method "my_#{state}_comments" do
        my_comments.with_state state
      end

      define_method "#{state}_comcoms" do
        comcoms.with_state state
      end
    end

    # I think we shouldn't to have my_deleted_comments cache counter
    def recalculate_my_comments_counter!
      dcount = my_draft_comments.count
      pcount = my_published_comments.count
      update_attributes!({
        my_draft_comments_count:     dcount,
        my_published_comments_count: pcount,
        my_comments_count:           dcount + pcount
      })
    end

    def recalculate_comcoms_counters!
      update_attributes!({
        draft_comcoms_count:     draft_comcoms.count,
        published_comcoms_count: published_comcoms.count,
        deleted_comcoms_count:   deleted_comcoms.count
      })
    end

    def update_comcoms_spam_counter
      update!(spam_comcoms_count: comcoms.where(spam: true).count)
    end

    def comments_sum
      published_comments_count + draft_comments_count
    end

    def comcoms_sum
      published_comcoms_count + draft_comcoms_count
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
the_comments-2.2.2 app/models/concerns/user.rb
the_comments-2.2.1 app/models/concerns/user.rb
the_comments-2.2.0 app/models/concerns/user.rb
the_comments-2.1.0 app/models/concerns/user.rb
the_comments-2.0.1 app/models/concerns/user.rb
the_comments-2.0.0 app/models/concerns/user.rb