app/models/editable_components/block.rb in editable_components-0.1.0 vs app/models/editable_components/block.rb in editable_components-0.1.2
- old
+ new
@@ -13,10 +13,11 @@
accepts_nested_attributes_for :ec_blocks, allow_destroy: true
accepts_nested_attributes_for :items
# --- hooks -------------------------------------------------------------- #
before_create :on_before_create
+ after_create :on_after_create
# --- scopes ------------------------------------------------------------- #
default_scope { order( position: :desc ) }
scope :published, -> { where( published: true ) unless EditableComponents::Engine.edit_mode }
scope :with_nested, -> { includes( :items, ec_blocks: :items ) }
@@ -83,37 +84,10 @@
items.each { |item| @_items[item.name] = item }
end
@_items[name]
end
- def init
- t = block_type.to_sym
- if Block::block_types.include? t
- init_items self, EditableComponents.config[:ec_blocks][t][:items]
- end
- end
-
- def init_items( block, items )
- items.each do |name, type|
- t = type.to_sym
- if type.to_s.start_with? 'item_'
- c = 'EditableComponents::' + ActiveSupport::Inflector.camelize( t )
- begin
- model = c.constantize
- rescue Exception => e
- Rails.logger.error '[ERROR] EditableComponents - init_items: ' + e.message
- model = false
- end
- block.items << model.new( name: name ).init if model
- elsif Block::block_types.include? t.to_sym
- cmp = Block.new( block_type: t, name: name )
- block.ec_blocks << cmp
- init_items( cmp, EditableComponents.config[:ec_blocks][t][:items] )
- end
- end if items
- end
-
def has_parent?
parent.present?
end
def has_children?
@@ -122,24 +96,26 @@
def is_sub_block?
parent.present? && parent_type == 'EditableComponents::Block'
end
+ def on_after_create
+ # TODO: validates type before creation!
+ t = self.block_type.to_sym
+ Block::init_items( self, EditableComponents.config[:ec_blocks][t][:items] ) if Block::block_types.include?( t )
+ end
+
def on_before_create
- if self._init
- self._init = false
- if self.name.blank?
- names = parent.ec_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
+ if self.name.blank?
+ names = parent.ec_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
- init
end
end
def props
pieces = {}
@@ -169,8 +145,26 @@
OpenStruct.new( pieces )
end
def self.block_types
@@block_types ||= EditableComponents.config[:ec_blocks].keys
+ end
+
+ def self.init_items( block, items )
+ items.each do |name, type|
+ t = type.to_sym
+ if type.to_s.start_with? 'item_'
+ c = 'EditableComponents::' + ActiveSupport::Inflector.camelize( t )
+ begin
+ model = c.constantize
+ rescue Exception => e
+ Rails.logger.error '[ERROR] EditableComponents - 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.ec_blocks << ( cmp = Block.new( block_type: t, name: name ) )
+ end
+ end if items
end
end
end