Sha256: 4525819394a0429f7475ee3ede53f5bd4d9468d9127565c7ed6d3bc7816a68c2

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

class Area < ActiveRecord::Base
  include Applicat::Mvc::Model::Resource::Base
  include Applicat::Mvc::Model::Tree
  include Applicat::Mvc::Model::Tokenable
  
  has_and_belongs_to_many :users
  has_and_belongs_to_many :projects
  
  scope :with_projects_for_product, ->(product_id) do
    select('DISTINCT(areas.id), areas.*').joins(%Q{
      LEFT JOIN areas areas2 ON areas2.id = areas.id OR areas2.ancestry like CONCAT(areas.id, '/', '%') 
        OR areas2.ancestry like CONCAT('%', '/', areas.id) 
        OR areas2.ancestry like CONCAT('%', '/', areas.id, '/', '%')  
        OR areas2.ancestry = CONCAT(areas.id, '') 
      LEFT JOIN areas_projects ON areas_projects.area_id = areas2.id 
      LEFT JOIN projects ON projects.id = areas_projects.project_id
    }).where('areas2.id IS NOT NULL AND projects.product_id = ?', product_id)
  end
  
  validates :name, presence: true, uniqueness: true
  
  attr_accessible :name, :parent_id
  
  extend FriendlyId
  
  friendly_id :name, :use => :slugged
  
  def self.find_by_product_id(product_id)
    #roots.joins(:projects).merge(Project.for_product_id(product_id))
    roots.with_projects_for_product(product_id)
  end
  
  def children_for_product_id(product_id)
    #children.joins(:projects).merge(Project.for_product_id(product_id))
    children.with_projects_for_product(product_id)
  end
    
  def products
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
voluntary-0.4.0 app/models/area.rb
voluntary-0.3.0 app/models/area.rb