class Writefully::Taxon::EagerLoader

Attributes

resource[R]
taggings[R]
tags[R]

Public Class Methods

new(klass) click to toggle source
# File lib/writefully/taxon.rb, line 39
def initialize(klass)
  @tags      = Tag.arel_table
  @taggings  = Tagging.arel_table
  @resource  = klass.arel_table
end

Public Instance Methods

build(*types) click to toggle source
# File lib/writefully/taxon.rb, line 45
def build *types
  [resource[Arel.star]] << types.map { |type| array_of_taxonomy_hstores_for(type) }
end

Private Instance Methods

array_of_taxonomy_hstores_for(type) click to toggle source
# File lib/writefully/taxon.rb, line 51
def array_of_taxonomy_hstores_for type
  Arel::Nodes::NamedFunction.new('ARRAY', [hstore_for_taxon(type)]).as("all_#{type}")
end
calculate_type(type) click to toggle source
# File lib/writefully/taxon.rb, line 74
def calculate_type type
  type == :tags ? nil : type.to_s.classify 
end
hstore_for_taxon(type) click to toggle source
# File lib/writefully/taxon.rb, line 55
def hstore_for_taxon type
  tags.project(Arel.sql('hstore(taxon)')).from(taxons_for_resource(type))
end
taggings_by_resource() click to toggle source
# File lib/writefully/taxon.rb, line 68
def taggings_by_resource
  # we use post_id for now but we should
  # make it a polymorphic association later
  taggings[:post_id].eq(resource[:id])
end
tags_by_type(type) click to toggle source
# File lib/writefully/taxon.rb, line 64
def tags_by_type type
  tags[:type].eq(calculate_type(type))
end
taxons_for_resource(type) click to toggle source
# File lib/writefully/taxon.rb, line 59
def taxons_for_resource type
  Tag.joins(:taggings).arel.where(tags_by_type(type))
                           .where(taggings_by_resource).as('taxon')
end