Sha256: 9aa2a080684ede48f79a349b3adb7fd1242581bd6a175b509c4e807c743a4a36

Contents?: true

Size: 693 Bytes

Versions: 3

Compression:

Stored size: 693 Bytes

Contents

class Tag < ActiveRecord::Base
  def ==(other)
    self.class.table_name == other.class.table_name && self.id == other.id
  end

  def posts
    post_scope = ->(tag) { where '? = ANY (posts.tags)', tag.id }
    options = {}
    reflection = ActiveRecord::Reflection::HasManyReflection.new(:posts, post_scope, options, self.class)
    association = PostsAssociation.new(self, reflection)
    @posts ||= ActiveRecord::Associations::CollectionProxy.new(Post, association)
  end

  private

  class PostsAssociation < ActiveRecord::Associations::HasManyAssociation
    def scope(opts = {})
      Post.where('? = ANY (posts.tags)', owner.id).published.order('published_at DESC')
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ecrire-0.25.2 lib/ecrire/app/models/tag.rb
ecrire-0.25.1 lib/ecrire/app/models/tag.rb
ecrire-0.25.0 lib/ecrire/app/models/tag.rb