Sha256: 60d1bfe4edc76436e9a8fb4fd30916e949ea4b8b12fd40e256d1fd5cc301477e

Contents?: true

Size: 1.6 KB

Versions: 16

Compression:

Stored size: 1.6 KB

Contents

module Alchemy
  module Custom
    module Model
      class Cloner < ::ActiveType::Object



        attribute :language_id, :integer
        attribute :site_id, :integer

        validates :language_id, :site_id, presence: true
        validate :check_lang_in_site, unless: -> {site_id.blank?}


        ###
        # ritorna true se il clone รจ andato a buon fine false altrimenti
        def apply
          if valid?
            cloned = self.send(self.to_cloner_name).clone_to_other_lang(get_attributes_for_clone.with_indifferent_access)
            if cloned.valid?
              true
            else
              cloned.errors.full_messages.each do | error|
                self.errors.add(:base, error)
              end
              false
            end
          else
            false
          end
        end



        private
        
        class << self
          def cloner_of(name,scope = nil, options={})
            class_attribute :to_cloner_name
            name = name.to_sym
            if options[:foreign_key]
              attribute options[:foreign_key]
            else
              attribute :"#{name}_id"
            end
            belongs_to name, scope, options
            self.to_cloner_name = name
          end
        end
        
        def get_attributes_for_clone
          self.attributes
        end
        
        
        protected

        def check_lang_in_site
          site = Alchemy::Site.find self.site_id
          if !site.languages.pluck(:id).include? self.language_id
            errors.add(:language_id, :isnt_in_site)
          end
        end

      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
alchemy-custom-model-3.2 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-3.1.5 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-3.1.4 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-3.1.3 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-3.1.2 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-3.1.1 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-3.1.0 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-2.2.2 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-3.0.0 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-2.2.1 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-2.2.0 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-2.1.2 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-2.1.1 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-2.1.0 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-2.0.3 app/models/alchemy/custom/model/cloner.rb
alchemy-custom-model-2.0.2 app/models/alchemy/custom/model/cloner.rb