Class: ContentsCore::Block

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

Constant Summary collapse

EMPTY_DATA =
OpenStruct.new( { data: '' } )

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.block_typesObject



164
165
166
# File 'app/models/contents_core/block.rb', line 164

def self.block_types
  @@block_types ||= ContentsCore.config[:cc_blocks].keys
end

.init_items(block, items) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/models/contents_core/block.rb', line 168

def self.init_items( block, items )
  items.each do |name, type|
    t = type.to_sym
    if type.to_s.start_with? 'item_'
      c = 'ContentsCore::' + ActiveSupport::Inflector.camelize( t )
      begin
        model = c.constantize
      rescue Exception => e
        Rails.logger.error '[ERROR] ContentsCore - init_items: ' + e.message
        model = false
      end
      block.items << model.new( name: name ).init if model
    elsif Block::block_types.include? t.to_sym
      block.cc_blocks << ( cmp = Block.new( block_type: t, name: name ) )
    end
  end if items
end

Instance Method Details

#attr_idObject

after_validation :on_after_validation

field :block_type, type: String, default: ‘text’ field :name, type: String, default: ” field :position, type: Integer, default: 0 field :published, type: Mongoid::Boolean, default: true field :_init, type: Mongoid::Boolean, default: false

embedded_in :parent, polymorphic: true

embeds_many :cc_blocks, cascade_callbacks: true, order: :position.desc, class_name: ‘ContentsCore::Block’ embeds_many :items, cascade_callbacks: true, class_name: ‘ContentsCore::Item’

accepts_nested_attributes_for :cc_blocks, allow_destroy: true accepts_nested_attributes_for :items

# scope :published, -> { where( published: true ) unless ApplicationController.edit_mode }



54
55
56
# File 'app/models/contents_core/block.rb', line 54

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

#children_typeObject



58
59
60
# File 'app/models/contents_core/block.rb', line 58

def children_type
  ContentsCore.config[:cc_blocks][block_type.to_sym][:children_type]
end

#create_item(item_type, item_name = nil) ⇒ Object



62
63
64
65
66
67
# File 'app/models/contents_core/block.rb', line 62

def create_item( item_type, item_name = nil )
  new_item = ContentsCore::Item.new( type: item_type )
  new_item.name = item_name if item_name
  self.items << new_item
  new_item
end

#editableObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/contents_core/block.rb', line 69

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.children_type,
      '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 by name



88
89
90
91
92
93
94
# File 'app/models/contents_core/block.rb', line 88

def get( name )
  unless @_items
    @_items = {}
    items.each { |item| @_items[item.name] = item }
  end
  @_items[name] ? @_items[name].data : nil
end

#has_children?Boolean



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

def has_children?
  cc_blocks.exists?
end

#has_parent?Boolean



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

def has_parent?
  parent.present?
end

#is_sub_block?Boolean



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

def is_sub_block?
  parent.present? && parent_type == 'ContentsCore::Block'
end

#on_after_createObject



108
109
110
111
112
# File 'app/models/contents_core/block.rb', line 108

def on_after_create
  # TODO: validates type before creation!
  t = self.block_type.to_sym
  Block::init_items( self, ContentsCore.config[:cc_blocks][t][:items] ) if Block::block_types.include?( t )
end

#on_before_createObject



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

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

#propsObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/contents_core/block.rb', line 127

def props
  pieces = {}

  Item::item_types.each do |type|
    pieces[type.pluralize.to_sym] = []
  end
  items.each do |item|  # TODO: improve me
    pieces[item.class.type_name.pluralize.to_sym].push item
  end
  Item::item_types.each do |type|
    pieces[type.to_sym] = 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



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

def set( name, value )
  items.each do |item|
    if item.name == name
      item.data = value
      break
    end
  end
end