Sha256: 0e93cefc6d0fc513a009c7245f79fe7cd194718784416ae3e576fdd918401e8d

Contents?: true

Size: 1.29 KB

Versions: 10

Compression:

Stored size: 1.29 KB

Contents

←   [documentation](documentation.md)

### Commentable API

```ruby
class Post < ActiveRecord::Base
  include TheCommentsCommentable

  belongs_to :user

  def commentable_title
    try(:title) || "Undefined title" 
  end

  def commentable_url
    ['', self.class.to_s.tableize, id].join('/')
  end

  def commentable_state
    try(:state) || "published"
  end
end
```

```ruby
@post = Post.last

# Post owner
@post.user # => User

# All comments for commentable object
@post.comments # => ActiveRecord:Collection

# Cache counters
@post.draft_comments_count      # => 1
@post.published_comments_count  # => 2
@post.deleted_comments_count    # => 0

# equal values with direct request to database
@post.comments.with_state([:draft]).count      # => 1
@post.comments.with_state([:published]).count  # => 2
@post.comments.with_state([:deleted]).count    # => 0

# Alias for:
# draft_comments_count + published_comments_count
@post.comments_sum # => 3

# Spam comments
@post.comments.where(spam: true) # => ActiveRecord::Relation

# recalculate cache counters
@post.recalculate_comments_counters!

# Default Denormalization methods
# should be redefined by developer
@post.commentable_title   => "Maiores eos rerum numquam aut."
@post.commentable_url     => "/posts/9"
@post.commentable_state   => "published"
```

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
fuck_comments-2.3.4 docs/commentable_api.md
the_comments_ruby-2.3.4 docs/commentable_api.md
the_comments_ruby-2.3.3 docs/commentable_api.md
the_comments-2.3.1 docs/commentable_api.md
the_comments-2.2.2 docs/commentable_api.md
the_comments-2.2.1 docs/commentable_api.md
the_comments-2.2.0 docs/commentable_api.md
the_comments-2.1.0 docs/commentable_api.md
the_comments-2.0.1 docs/commentable_api.md
the_comments-2.0.0 docs/commentable_api.md