Class: EditableComponents::Block
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- EditableComponents::Block
- Defined in:
- app/models/editable_components/block.rb
Constant Summary collapse
- EMPTY_DATA =
OpenStruct.new( { data: '' } )
Class Method Summary collapse
Instance Method Summary collapse
-
#attr_id ⇒ Object
after_validation :on_after_validation.
- #children_type ⇒ Object
- #editable ⇒ Object
-
#get(name) ⇒ Object
Returns an item by name.
- #has_children? ⇒ Boolean
- #has_parent? ⇒ Boolean
- #is_sub_block? ⇒ Boolean
- #on_after_create ⇒ Object
- #on_before_create ⇒ Object
- #props ⇒ Object
Class Method Details
.block_types ⇒ Object
148 149 150 |
# File 'app/models/editable_components/block.rb', line 148 def self.block_types @@block_types ||= EditableComponents.config[:ec_blocks].keys end |
.init_items(block, items) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'app/models/editable_components/block.rb', line 152 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. 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 |
Instance Method Details
#attr_id ⇒ Object
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 :ec_blocks, cascade_callbacks: true, order: :position.desc, class_name: ‘EditableComponents::Block’ embeds_many :items, cascade_callbacks: true, class_name: ‘EditableComponents::Item’
accepts_nested_attributes_for :ec_blocks, allow_destroy: true accepts_nested_attributes_for :items
# scope :published, -> { where( published: true ) unless ApplicationController.edit_mode }
54 55 56 |
# File 'app/models/editable_components/block.rb', line 54 def attr_id "#{self.class.to_s.split('::').last}-#{self.id}" end |
#children_type ⇒ Object
58 59 60 |
# File 'app/models/editable_components/block.rb', line 58 def children_type EditableComponents.config[:ec_blocks][block_type.to_sym][:children_type] end |
#editable ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/editable_components/block.rb', line 62 def editable Engine.edit_mode ? ( 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
81 82 83 84 85 86 87 |
# File 'app/models/editable_components/block.rb', line 81 def get( name ) unless @_items @_items = {} items.each { |item| @_items[item.name] = item } end @_items[name] end |
#has_children? ⇒ Boolean
93 94 95 |
# File 'app/models/editable_components/block.rb', line 93 def has_children? ec_blocks.exists? end |
#has_parent? ⇒ Boolean
89 90 91 |
# File 'app/models/editable_components/block.rb', line 89 def has_parent? parent.present? end |
#is_sub_block? ⇒ Boolean
97 98 99 |
# File 'app/models/editable_components/block.rb', line 97 def is_sub_block? parent.present? && parent_type == 'EditableComponents::Block' end |
#on_after_create ⇒ Object
101 102 103 104 105 |
# File 'app/models/editable_components/block.rb', line 101 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 |
#on_before_create ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/models/editable_components/block.rb', line 107 def on_before_create 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 end end |
#props ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/models/editable_components/block.rb', line 120 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 |