app/models/kuhsaft/brick.rb in kuhsaft-2.2.6 vs app/models/kuhsaft/brick.rb in kuhsaft-2.3.0

- old
+ new

@@ -1,25 +1,35 @@ module Kuhsaft class Brick < ActiveRecord::Base include Kuhsaft::BrickList - belongs_to :brick_list, :polymorphic => true, :touch => true + belongs_to :brick_list, polymorphic: true, touch: true - scope :localized, -> { where(:locale => I18n.locale) } - default_scope -> { order('position ASC').localized } + scope :localized, -> { where(locale: I18n.locale) } + default_scope { order('position ASC').localized } serialize :display_styles, Array before_validation :set_position validates :locale, :position, :type, :brick_list_id, :brick_list_type, - :presence => true + presence: true + validates :template_name, + :type, + :locale, + :caption, + :link_style, + :image_size, + :video, + :alt_text, + length: { maximum: 255 } + after_initialize do self.position ||= has_siblings? ? brick_list.bricks.maximum(:position).to_i + 1 : 1 end after_save do @@ -37,28 +47,26 @@ brick_list.brick_list.update_fulltext end end def to_edit_partial_path - path = self.to_partial_path.split '/' + path = to_partial_path.split '/' path << 'edit' path.join '/' end def has_siblings? - if brick_list - brick_list.bricks.any? - end + brick_list.present? && brick_list.bricks.any? end # # The child partial can contain your own implementation # of how the brick renders it's child in the edit form. # Returns the path to this partial. # def to_edit_childs_partial_path - path = self.to_partial_path.split '/' + path = to_partial_path.split '/' path << 'childs' path.join '/' end def parents @@ -72,25 +80,25 @@ p.reverse end def set_position self.position = if self.position.present? - self.position - elsif self.respond_to?(:brick_list) && self.brick_list.respond_to?(:bricks) - brick_list.bricks.maximum(:position).to_i + 1 - else - 1 - end + self.position + elsif self.respond_to?(:brick_list) && brick_list.respond_to?(:bricks) + brick_list.bricks.maximum(:position).to_i + 1 + else + 1 + end end def brick_list_type 'Kuhsaft::Brick' end # Returns a css classname suitable for use in the frontend def to_style_class - ([self.class.to_s.underscore.dasherize.gsub('/', '-')] + self.display_styles).join(' ') + ([self.class.to_s.underscore.dasherize.gsub('/', '-')] + display_styles).join(' ') end # Returns a unique DOM id suitable for use in the frontend def to_style_id "#{self.class.to_s.underscore.dasherize.gsub('/', '-')}-#{id}" @@ -99,15 +107,29 @@ # return a list of css classnames that can be applied to the brick def available_display_styles [] end + def translated_available_display_styles + available_display_styles.map do |style| + [I18n.t("#{self.class.to_s.demodulize.underscore}.display_styles.#{style}"), style] + end + end + def backend_label(options = {}) label = self.class.model_name.human if options[:parenthesis] == true "(#{label})" else label end + end + + def partial_digest(name) + ActionView::Digestor.digest(name, 'haml', ApplicationController.new.lookup_context, partial: true) + end + + def cache_key + super + partial_digest(to_partial_path) end end end