Sha256: 64568f1a2a1bfe5639bc01109772d6fb28c4ce9f81075ceb18f6b8f3369a5a41

Contents?: true

Size: 1.75 KB

Versions: 6

Compression:

Stored size: 1.75 KB

Contents

module Concerns
  module Model
    module BaseThing
      extend ActiveSupport::Concern
      
      included do
        # find or create movies list
        class_attribute :list_instance
      
        #LIST_ID = list.id
      
        has_many :list_items, as: :thing, dependent: :destroy
        has_many :user_list_items, as: :thing
        has_many :users, through: :user_list_items
        
        attr_accessible :name
        
        validates :name, presence: true, uniqueness: true
        validate :special_characters_excluded
        
        #pusherable "#{Rails.env}_channel"
      end
      
      module ClassMethods
        def list
          #if self.to_s.constantize.const_defined?('LIST_ID') && LIST_ID.present?
          #  self.to_s.constantize.list_instance ||= List.find(LIST_ID)
          #end
          
          return self.to_s.constantize.list_instance if self.to_s.constantize.list_instance.present?
          
          attributes = {
            adjective: 'best', topic: self.to_s, scope: 'ever', thing_type: self.to_s,
            negative_adjective: 'worst'
          }
          
          self.to_s.constantize.list_instance = List.where(attributes).first
          
          unless self.to_s.constantize.list_instance.present?
            self.to_s.constantize.list_instance = List.create!(attributes)      
          end
          
          self.to_s.constantize.list_instance
        end
      end
      
      # custom associations
      def lists; List.where(thing_type: self.to_s); end
      
      def special_characters_excluded
        if name.match(/\//)
          errors[:name] << I18n.t(
            'activerecord.errors.models.thing.attributes.name.unwanted_special_characters_included'
          )
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
voluntary-0.7.1 lib/concerns/model/base_thing.rb
voluntary-0.7.0 lib/concerns/model/base_thing.rb
voluntary-0.6.0 lib/concerns/model/base_thing.rb
voluntary-0.5.2 lib/concerns/model/base_thing.rb
voluntary-0.5.1 lib/concerns/model/base_thing.rb
voluntary-0.5.0 lib/concerns/model/base_thing.rb