Class: EasyTags::TaggableContextMethods

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_tags/taggable_context_methods.rb

Overview

Handles injecting of the dynamic context related methods Example:

easy_tags_on :bees

# will create:

has_many :bees_taggings
has_many :bees_tags

def bees
def bees=
def bees_list
def bees_list=

Class Method Summary collapse

Class Method Details

.inject(class_instance:, context:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/easy_tags/taggable_context_methods.rb', line 19

def inject(class_instance:, context:)
  class_instance.class_eval "    has_many(\n      :\#{context}_taggings, -> { includes(:tag).where(context: :\#{context}) },\n      as: :taggable,\n      class_name: 'EasyTags::Tagging',\n      dependent: :destroy\n    )\n\n    has_many(\n      :\#{context}_tags,\n      class_name: 'EasyTags::Tag',\n      through: :\#{context}_taggings,\n      source: :tag\n    )\n\n    attribute :\#{context}_list, ActiveModel::Type::Value.new\n\n    def \#{context}\n      _taggable_context(:\#{context})\n    end\n\n    def \#{context}=(value)\n      _taggable_context(:\#{context}).update(value)\n      \#{context}\n    end\n\n    def \#{context}_list\n      _taggable_context(:\#{context}).tags.to_s\n    end\n\n    def \#{context}_list=(value)\n      _taggable_context(:\#{context}).update(value)\n      \#{context}_list\n    end\n\n    def \#{context}_list_persisted\n      _taggable_context(:\#{context}).persisted_tags.to_s\n    end\n  RUBY\nend\n", __FILE__, __LINE__ + 1 # rubocop:disable Style/DocumentDynamicEvalDefinition