Module: Seorel::Model::Base

Defined in:
lib/seorel/model/base.rb

Instance Method Summary collapse

Instance Method Details

#seorelify(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/seorel/model/base.rb', line 5

def seorelify(*args)
  extend  ClassMethods
  include InstanceMethods

  cattr_accessor :seorel_title_field, :seorel_description_field, :seorel_image_field

  if args[0].class.name == 'Hash'
    class_variable_set '@@seorel_title_field',       args[0][:title]
    class_variable_set '@@seorel_description_field', args[0][:description] || args[0][:title]
    class_variable_set '@@seorel_image_field',       args[0][:image]
  else
    class_variable_set '@@seorel_title_field',       args[0]
    class_variable_set '@@seorel_description_field', args[1] || args[0]
    class_variable_set '@@seorel_image_field',       args[2]
  end

  has_one :seorel, as: :seorelable, dependent: :destroy, class_name: 'Seorel::Seorel'
  accepts_nested_attributes_for :seorel, allow_destroy: true

  delegate :title, :title?, :description, :description?, :image, :image?,
    to: :seorel, prefix: :seo, allow_nil: true

  before_save :set_seorel
end