Class: ContentsCore::Item

Inherits:
ApplicationRecord show all
Defined in:
app/models/contents_core/item.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.permitted_attributesObject



101
102
103
# File 'app/models/contents_core/item.rb', line 101

def self.permitted_attributes
  [:data_boolean, :data_datetime, :data_file, :data_float, :data_hash, :data_integer, :data_string, :data_text]
end

.type_nameObject



105
106
107
# File 'app/models/contents_core/item.rb', line 105

def self.type_name
  ''
end

.typesObject



109
110
111
# File 'app/models/contents_core/item.rb', line 109

def self.types
  @@types ||= ContentsCore.config[:items].keys.map( &:to_sym )
end

Instance Method Details

#as_json(options = nil) ⇒ Object



25
26
27
# File 'app/models/contents_core/item.rb', line 25

def as_json( options = nil )
  super( {only: [:id, :name, :type], methods: [:data]}.merge(options || {}) )
end

#attr_idObject



29
30
31
# File 'app/models/contents_core/item.rb', line 29

def attr_id
  "#{self.class_name}-#{self.id}"
end

#class_nameObject



33
34
35
# File 'app/models/contents_core/item.rb', line 33

def class_name
  self.class.to_s.split( '::' ).last
end

#configObject



37
38
39
# File 'app/models/contents_core/item.rb', line 37

def config
  @config ||= ( ContentsCore.config[:items] && ContentsCore.config[:items][self.class_name.underscore.to_sym] ? ContentsCore.config[:items][self.class_name.underscore.to_sym] : {} ).merge( self.block && self.block.config[:options] && self.name && self.block.config[:options][self.name.to_sym] ? self.block.config[:options][self.name.to_sym] : {} )
end

#data_typeObject



41
42
43
# File 'app/models/contents_core/item.rb', line 41

def data_type
  @data_type ||= ( config[:data_type] || :string ).to_sym
end

#editableObject



45
46
47
# File 'app/models/contents_core/item.rb', line 45

def editable
  ContentsCore.editing ? " data-ec-item=\"#{self.id}\" data-ec-input=\"#{self.opt_input}\" data-ec-type=\"#{self.class_name}\"".html_safe : ''
end

#initObject

placeholder method (for override)



49
50
# File 'app/models/contents_core/item.rb', line 49

def init  # placeholder method (for override)
end

#on_after_initializeObject

— methods ————————————————————-



20
21
22
23
# File 'app/models/contents_core/item.rb', line 20

def on_after_initialize
  self.data = config[:default] if config[:default] && !self.data
  self.init
end

#on_before_createObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/contents_core/item.rb', line 52

def on_before_create
  # root_block = self.block
  # root_block = root_block.parent while root_block.parent.is_a? Block
  # names = Block.items_keys root_block.tree
  names = ( self.block.items - [self] ).pluck :name
  if self.name.blank? || names.include?( self.name )  # Search a not used name
    n = self.name.blank? ? self.class.type_name : self.name
    i = 0
    while( ( i += 1 ) < 1000 )
      unless names.include? "#{n}-#{i}"
        self.name = "#{n}-#{i}"
        break
      end
    end
  end
end

#opt_inputObject



69
70
71
72
73
74
75
76
77
# File 'app/models/contents_core/item.rb', line 69

def opt_input
  if self.block.config[self.name] && self.block.config[self.name]['input']
    self.block.config[self.name]['input'].to_s
  elsif config[:input]
    config[:input].to_s
  else
    ''
  end
end

#process_data(args = nil) ⇒ Object



79
80
81
# File 'app/models/contents_core/item.rb', line 79

def process_data( args = nil )
  config[:process_data].call( self.data, args ) if config[:process_data]
end

#set(value) ⇒ Object



83
84
85
86
# File 'app/models/contents_core/item.rb', line 83

def set( value )
  self.data = value
  self
end

#to_sObject



88
89
90
# File 'app/models/contents_core/item.rb', line 88

def to_s
  self.data.to_s
end

#update_data(value) ⇒ Object



92
93
94
95
# File 'app/models/contents_core/item.rb', line 92

def update_data( value )
  self.data = value
  self.save
end

#validate_itemObject



97
98
99
# File 'app/models/contents_core/item.rb', line 97

def validate_item
  config[:validate].call( self ) if config[:validate]
end