Class: ContentsCore::Block

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, &block) ⇒ Block

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



59
60
61
62
63
64
65
# File 'app/models/contents_core/block.rb', line 59

def initialize( attributes = {}, &block )
  super( attributes, &block )
  @create_children = 0
  self.conf = {} unless self.conf
  self.group = config[:group]
  self.block_type = parent.config[:new_children] if attributes[:block_type].nil? && self.parent_type == 'ContentsCore::Block'
end

Instance Attribute Details

#create_childrenObject

— misc —————————————————————-



7
8
9
# File 'app/models/contents_core/block.rb', line 7

def create_children
  @create_children
end

Class Method Details

.enum(include_children = true) ⇒ Object



211
212
213
# File 'app/models/contents_core/block.rb', line 211

def self.enum( include_children = true )
  ContentsCore.config[:blocks].map{|k, v| [I18n.t( 'contents_core.blocks.' + v[:name].to_s ), k.to_s] if !include_children || !v[:child_only]}.compact.sort_by{|b| b[0]}
end

.initialize_children(block, children, options = {}) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/models/contents_core/block.rb', line 215

def self.initialize_children( block, children, options = {} )
  children.each do |name, type|
    t = type.to_sym
    if Item.types.include? t
      c = 'ContentsCore::' + ActiveSupport::Inflector.camelize( t )
      begin
        model = c.constantize
      rescue Exception => e
        Rails.logger.error '[ERROR] ContentsCore - initialize_children: ' + e.message
        model = false
      end
      if model
        block.items.new( type: model.name, name: name )
      end
    elsif Block.types( false ).include? t
      block.create_children.times do
        block.cc_blocks.new( block_type: t, name: name )
        block.save
      end
    end
  end if children
  block.save
end

.items_keys(keys) ⇒ Object



239
240
241
242
243
# File 'app/models/contents_core/block.rb', line 239

def self.items_keys( keys )
  keys.map do |k, v|
    v.is_a?( Hash ) ? items_keys( v ) : k
  end.flatten
end

.permitted_attributesObject



245
246
247
# File 'app/models/contents_core/block.rb', line 245

def self.permitted_attributes
  [ :id, :name, :block_type, :position, :_destroy, items_attributes: [ :id ] + Item::permitted_attributes, cc_blocks_attributes: [ :id, :name, :block_type, items_attributes: [ :id ] + Item::permitted_attributes ] ]
end

.types(include_children = true) ⇒ Object



249
250
251
# File 'app/models/contents_core/block.rb', line 249

def self.types( include_children = true )
  @@types ||= ContentsCore.config[:blocks].select{|k, v| !include_children || !v[:child_only]}.keys.map( &:to_sym )
end

Instance Method Details

#as_json(options = nil) ⇒ Object



67
68
69
# File 'app/models/contents_core/block.rb', line 67

def as_json( options = nil )
  super({ only: [:id, :block_type, :name, :group, :position, :published], include: [:cc_blocks, :items]}.merge(options || {}))
end

#attr_idObject



71
72
73
# File 'app/models/contents_core/block.rb', line 71

def attr_id
  "#{self.class.to_s.split('::').last}-#{self.id}"
end

#configObject



75
76
77
# File 'app/models/contents_core/block.rb', line 75

def config
  !self.conf.blank? ? self.conf : ( ContentsCore.config[:blocks][block_type.to_sym] ? ContentsCore.config[:blocks][block_type.to_sym] : {} )
end

#create_item(item_type, options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/contents_core/block.rb', line 79

def create_item( item_type, options = {} )
  if ContentsCore.config[:items].keys.include? item_type
    attrs = { type: "ContentsCore::#{item_type.to_s.classify}" }  # TODO: check if model exists
    attrs[:name] = options[:name]  if options[:name]
    attrs[:data] = options[:value] if options[:value]
    item = self.items.new attrs
    item.save
    item
  else
    raise "Invalid item type: #{item_type} - check defined items in config"
  end
end

#editableObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/contents_core/block.rb', line 92

def editable
  ContentsCore.editing ? (
    is_sub_block? ?
    {
      'data-ec-sub-block': self.id,
      'data-ec-ct': self.block_type,
      'data-ec-position': self.position,
      'data-ec-pub': self.published
    } :
    {
      'data-ec-block': self.id,
      'data-ec-container': self.new_children,
      'data-ec-ct': self.block_type,
      'data-ec-pub': self.published
    }
  ).map { |k, v| "#{k}=\"#{v}\"" }.join( ' ' ).html_safe : ''
end

#get(name) ⇒ Object

Returns an item value by name



111
112
113
114
# File 'app/models/contents_core/block.rb', line 111

def get( name )
  item = get_item( name )
  item && item.is_a?( Item ) ? item.data : nil
end

#get_item(name) ⇒ Object

Returns an item by name



117
118
119
120
121
122
123
124
# File 'app/models/contents_core/block.rb', line 117

def get_item( name )
  t = tree
  name.split( '.' ).each do |tok|
    return nil unless t[tok]
    t = t[tok]
  end
  t
end

#has_children?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'app/models/contents_core/block.rb', line 130

def has_children?
  cc_blocks.exists?
end

#has_parent?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'app/models/contents_core/block.rb', line 126

def has_parent?
  parent.present?
end

#is_sub_block?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'app/models/contents_core/block.rb', line 134

def is_sub_block?
  self.parent.present? && self.parent.is_a?( Block )
end

#new_childrenObject



138
139
140
# File 'app/models/contents_core/block.rb', line 138

def new_children
  config[:new_children]
end

#on_after_createObject



142
143
144
145
# File 'app/models/contents_core/block.rb', line 142

def on_after_create
  # TODO: validates type before creation!
  Block.initialize_children( self, config[:children] ) if Block.types( false ).include?( self.block_type.to_sym )
end

#on_before_createObject

def on_after_initialize

self.conf = {} unless self.conf

end



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/contents_core/block.rb', line 151

def on_before_create
  names = parent.cc_blocks.map( &:name )
  if self.name.blank? || names.include?( self.name )  # Search a not used name
    n = self.name.blank? ? block_type : self.name
    i = 0
    while( ( i += 1 ) < 1000 )
      unless names.include? "#{n}-#{i}"
        self.name = "#{n}-#{i}"
        break
      end
    end
  end
end

#propsObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/models/contents_core/block.rb', line 165

def props
  pieces = {}

  Item.types.each do |type|
    pieces[type.to_s.pluralize.to_sym] = []
  end
  items.each do |item|  # TODO: improve me
    pieces[item.class.type_name.pluralize.to_sym] = [] unless pieces[item.class.type_name.pluralize.to_sym]
    pieces[item.class.type_name.pluralize.to_sym].push item
  end
  Item.types.each do |type|
    pieces[type] = pieces[type.pluralize.to_sym].any? ? pieces[type.pluralize.to_sym].first : nil  # EMPTY_DATA - empty Item per sti class?
  end

  # pieces = {
  #   images:   items.select { |item| item.type == ItemImage.to_s },
  #   integers: items.select { |item| item.type == ItemInteger.to_s },
  #   strings:  items.select { |item| item.type == ItemString.to_s },
  #   texts:    items.select { |item| item.type == ItemText.to_s },
  # }

  # pieces[:image]  = pieces[:images].any?  ? pieces[:images].first  : EMPTY_DATA
  # pieces[:integers]  = pieces[:integers].any?  ? pieces[:integers].first  : EMPTY_DATA
  # pieces[:string] = pieces[:strings].any? ? pieces[:strings].first : EMPTY_DATA
  # pieces[:text]   = pieces[:texts].any?   ? pieces[:texts].first   : EMPTY_DATA

  OpenStruct.new( pieces )
end

#set(name, value) ⇒ Object



194
195
196
197
# File 'app/models/contents_core/block.rb', line 194

def set( name, value )
  item = get_item( name )
  item && item.is_a?( Item ) ? item.data = value : nil
end

#treeObject



199
200
201
202
203
204
205
# File 'app/models/contents_core/block.rb', line 199

def tree
  # return @items_tree if @items_tree
  @items_tree = {}  # prepare a complete list of items
  self.items.each{ |item| @items_tree[item.name] = item }
  self.cc_blocks.each_with_index{ |block, i| @items_tree[block.name] = block.tree }  # @items_tree[i] = block.tree
  @items_tree
end

#validationsObject



207
208
209
# File 'app/models/contents_core/block.rb', line 207

def validations
  config[:validations] || {}
end