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

.item_typesObject



53
54
55
# File 'app/models/contents_core/item.rb', line 53

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

.permitted_attributesObject



57
58
59
# File 'app/models/contents_core/item.rb', line 57

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

Instance Method Details

#as_json(options = nil) ⇒ Object



9
10
11
# File 'app/models/contents_core/item.rb', line 9

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

#attr_idObject



13
14
15
# File 'app/models/contents_core/item.rb', line 13

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

#class_nameObject



17
18
19
# File 'app/models/contents_core/item.rb', line 17

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

#configObject



61
62
63
# File 'app/models/contents_core/item.rb', line 61

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

#data_typeObject



65
66
67
# File 'app/models/contents_core/item.rb', line 65

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

#editableObject



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

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

#opt_inputObject



25
26
27
28
29
30
31
32
33
# File 'app/models/contents_core/item.rb', line 25

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

#process_data(args = nil) ⇒ Object



35
36
37
# File 'app/models/contents_core/item.rb', line 35

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

#set(value) ⇒ Object



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

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

#to_sObject



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

def to_s
  self.data
end

#update_data(value) ⇒ Object



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

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