Module: Writefully::Postable::ClassMethods

Defined in:
lib/writefully/postable.rb

Instance Method Summary collapse

Instance Method Details

#by_taxonomies(taxonomies, type) ⇒ Object



64
65
66
67
68
69
# File 'lib/writefully/postable.rb', line 64

def by_taxonomies(taxonomies, type)
  joins(type.to_sym)
  .where(writefully_tags: { slug: taxonomies, 
                            type: (type == "tags" ? nil : type.classify) })
  .uniq
end

#filter_with(filters) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/writefully/postable.rb', line 56

def filter_with(filters)
  if filters.present?
    filters.keys.inject(self) do |klass, type| 
      klass.where(id: by_taxonomies(filters[type].split(/,/), type))
    end
  else all end
end

#wf_content(field_name) ⇒ Object



71
72
73
74
75
# File 'lib/writefully/postable.rb', line 71

def wf_content(field_name)
  class_eval do 
    alias_attribute :content, :"#{field_name}" unless field_name == :content
  end
end

#wf_taxonomize(type, *args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/writefully/postable.rb', line 42

def wf_taxonomize(type, *args)
  class_eval do 
    has_many :"#{type}", *args

    define_method("all_#{type}") do 
      read_attribute(:"all_#{type}").map { |taxon| Hashie::Mash.new(taxon) }
    end

    define_method("#{type}=") do |tokens|
      self.taxonomize_with(tokens, type)
    end
  end
end