Class: ConstructorPages::Field

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
CodeNameUniq
Defined in:
app/models/constructor_pages/field.rb

Overview

Field model. Fields allows to add custom fields for template. Each field has type of value such as float, integer, string…

Constant Summary collapse

TYPES =

Array of available field types

%w{string integer float boolean text date html image}.tap {|_t|
_t.each {|t| class_eval %{has_many :#{t}_types, dependent: :destroy, class_name: 'Types::#{t.titleize}Type'}}}

Instance Method Summary collapse

Methods included from CodeNameUniq

#code_name_uniqueness

Instance Method Details

#create_type_object(page) ⇒ Object

Create object of type_value by page



34
# File 'app/models/constructor_pages/field.rb', line 34

def create_type_object(page); type_class.create(field_id: id, page_id: page.id) end

#find_or_create_type_object(page) ⇒ Object

Find or create type object by page



37
# File 'app/models/constructor_pages/field.rb', line 37

def find_or_create_type_object(page); find_type_object(page) || create_type_object(page) end

#find_type_object(page) ⇒ Object

Return object of type_value by page



31
# File 'app/models/constructor_pages/field.rb', line 31

def find_type_object(page); type_class.find_by(field_id: id, page_id: page.id) end

#get_value_for(page) ⇒ Object

Get value from type_field for specified page



43
# File 'app/models/constructor_pages/field.rb', line 43

def get_value_for(page); find_type_object(page).tap {|t| return t && t.value} end

#remove_type_object(page) ⇒ Object

Remove all type_fields values for specified page



40
# File 'app/models/constructor_pages/field.rb', line 40

def remove_type_object(page); find_type_object(page).destroy end

#set_value_for(page, value) ⇒ Object

Set value type_field for specified page



46
# File 'app/models/constructor_pages/field.rb', line 46

def set_value_for(page, value); find_type_object(page).tap {|t| t || return; t.update value: value} end

#type_classObject

Return constant of model type_value



28
# File 'app/models/constructor_pages/field.rb', line 28

def type_class; "constructor_pages/types/#{type_value}_type".classify.constantize end