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



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

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

.type_nameObject



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

def self.type_name
  ''
end

.typesObject



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

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

Instance Method Details

#as_json(options = nil) ⇒ Object



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

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

#attr_idObject



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

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

#class_nameObject



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

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

#configObject



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

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



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

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

#editableObject



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

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)



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

def init  # placeholder method (for override)
end

#on_after_initializeObject

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



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

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

#on_before_createObject



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

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



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

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



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

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

#set(value) ⇒ Object



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

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

#to_sObject



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

def to_s
  self.data
end

#update_data(value) ⇒ Object



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

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

#validate_itemObject



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

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