Sha256: 9cd016d9b3beb983943fd335098f7d2d24873f306726b26c4d64ff76871a1d40

Contents?: true

Size: 1.04 KB

Versions: 13

Compression:

Stored size: 1.04 KB

Contents

class List < ActiveRecord::Base
  attr_accessible :adjective, :topic, :scope, :thing_type, :negative_adjective
  
  has_many :items, class_name: 'ListItem', dependent: :destroy
  
  validates :adjective, presence: true
  validates :topic, presence: true, uniqueness: { scope: [:adjective, :scope] }
  validates :scope, presence: true
  
  #pusherable "#{Rails.env}_channel"
  
  def self.find_or_create_by_params(params)
    attributes = (params[:user_list_item] || params[:list_item] || params).clone
    attributes.symbolize_keys! unless params.is_a?(ActiveSupport::HashWithIndifferentAccess)
    
    if attributes[:list_id].present? then List.find(attributes[:list_id])
    elsif attributes[:adjective].present?
      list  = List.new
      
      attributes.each {|param, value| attributes.delete(param) unless list.respond_to?(param) }
      
      List.where(attributes).first || List.create(attributes)
    elsif attributes[:topic].present?
      begin 
        attributes[:topic].classify.constantize.list
      rescue NameError
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
voluntary-0.7.1 app/models/list.rb
voluntary-0.7.0 app/models/list.rb
voluntary-0.6.0 app/models/list.rb
voluntary-0.5.2 app/models/list.rb
voluntary-0.5.1 app/models/list.rb
voluntary-0.5.0 app/models/list.rb
voluntary-0.4.0 app/models/list.rb
voluntary-0.3.0 app/models/list.rb
voluntary-0.2.4 app/models/list.rb
voluntary-0.2.3 app/models/list.rb
voluntary-0.2.2 app/models/list.rb
voluntary-0.2.1 app/models/list.rb
voluntary-0.2.0 app/models/list.rb