Sha256: 43136777eee7509b8b59cd0acc2855dc59dba5a18c7df322ea8c322a01b8aaa5

Contents?: true

Size: 1.8 KB

Versions: 16

Compression:

Stored size: 1.8 KB

Contents

module Alchemy::Custom::Model
  module ModelDecoration
    extend ActiveSupport::Concern

    included do

      include GlobalIdSetter

      belongs_to :language, class_name: "Alchemy::Language"


      before_validation :set_current_language


      scope :only_current_language, -> {
        where(language_id: Alchemy::Language.current.id)
      }

      def to_url
        layout = Alchemy::PageLayout.get_all_by_attributes(custom_model: self.class.to_s).select {|ly| ly["custom_model_action"] == "show"}.first
        page = Alchemy::Language.current.pages.find_by(page_layout: layout["name"]).parent
        page.urlname
      end

      def ui_title
        self.class.to_s.demodulize.downcase
      end

      def breadcrumb_name
        self.class.to_s.demodulize.titleize
      end



      # module ClassMethods
      #   def custom_model *translate_fields, friendly: nil
      #     if !column_names.include? "site_id"
      #       raise "You have to add site_id column to your model"
      #     end
      #
      #       extend FriendlyId
      #
      #     unless translate_fields.empty?
      #       translates translate_fields
      #     end
      #
      #     unless friendly.nil?
      #       friendly_id friendly.to_sym, :use => [:globalize, :history]
      #     end
      #
      #   end
      # end

      private

      def set_current_language
        if self.language.nil?
          self.language = Alchemy::Language.current
        end
      end


    end


    module ClassMethods

      private

      def set_slug_if_present
        if column_names.include? "slug"
          validates :slug, uniqueness: true
          before_save :prevent_wrong_slug

          define_method :prevent_wrong_slug do
            self.slug = normalize_friendly_id self.slug
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
alchemy-custom-model-2.1.2 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-2.1.1 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-2.1.0 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-2.0.3 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-2.0.2 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-2.0.1 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-2.0.0 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-0.1.9 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-0.1.7 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-0.1.6 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-0.1.5 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-0.1.4 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-0.1.3 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-0.1.2 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-0.1.1 lib/alchemy/custom/model/model_decoration.rb
alchemy-custom-model-0.1.0 lib/alchemy/custom/model/model_decoration.rb