class Gutentag::ActiveRecord

Attributes

model[R]

Public Class Methods

call(model) click to toggle source
# File lib/gutentag/active_record.rb, line 4
def self.call(model)
  new(model).call
end
new(model) click to toggle source
# File lib/gutentag/active_record.rb, line 8
def initialize(model)
  @model = model
end

Public Instance Methods

call() click to toggle source
# File lib/gutentag/active_record.rb, line 12
def call
  add_associations
  add_callbacks
  add_methods
  add_attribute
end

Private Instance Methods

add_associations() click to toggle source
# File lib/gutentag/active_record.rb, line 23
def add_associations
  model.has_many :taggings,
    :class_name => "Gutentag::Tagging",
    :as         => :taggable,
    :dependent  => :destroy
  model.has_many :tags,
    :class_name => "Gutentag::Tag",
    :through    => :taggings
end
add_attribute() click to toggle source
# File lib/gutentag/active_record.rb, line 33
def add_attribute
  if ActiveRecord::VERSION::STRING.to_f <= 4.2
    model.define_attribute_method "tag_names"
  else
    model.attribute "tag_names", ActiveRecord::Type::Value.new,
      :default => default_tag_names
  end
end
add_callbacks() click to toggle source
# File lib/gutentag/active_record.rb, line 42
def add_callbacks
  model.after_save :persist_tags

  if legacy?
    model.after_save :reset_tag_names
  else
    model.after_commit :reset_tag_names, :on => %i[ create update ]
  end
end
add_methods() click to toggle source
# File lib/gutentag/active_record.rb, line 52
def add_methods
  case ActiveRecord::VERSION::STRING.to_f
  when 3.2..4.1
    require "gutentag/active_record/instance_methods_3_2"
  when 4.2
    require "gutentag/active_record/instance_methods_4_2"
  else
    require "gutentag/active_record/instance_methods"
  end

  model.send :extend, Gutentag::ActiveRecord::ClassMethods
  model.send :include, Gutentag::ActiveRecord::InstanceMethods
end
default_tag_names() click to toggle source
# File lib/gutentag/active_record.rb, line 66
def default_tag_names
  ActiveRecord::VERSION::STRING.to_f <= 4.2 ? [] : nil
end
legacy?() click to toggle source
# File lib/gutentag/active_record.rb, line 70
def legacy?
  ActiveRecord::VERSION::STRING.to_f < 4.2
end