Sha256: 91331d535065223eeefe73325c0a93b7846eb4f4293c1567cdce40e02d28920a

Contents?: true

Size: 646 Bytes

Versions: 2

Compression:

Stored size: 646 Bytes

Contents

Spree::Product.class_eval do
  attr_accessible :role_ids

  has_and_belongs_to_many :roles, :join_table => 'spree_restrictions'
    
  scope :by_roles, lambda{ |user_roles| 
    if(user_roles.include?(Spree::Role.find_by_name('admin')))
      where('1=1')
    else
      where('spree_products.id IN (
        SELECT p.id FROM spree_products p LEFT JOIN spree_restrictions r ON r.product_id = p.id WHERE r.product_id IS NULL
      )
      OR spree_products.id IN (
        SELECT p.id FROM spree_products p JOIN spree_restrictions r ON r.product_id = p.id WHERE role_id IN (?)
      )', user_roles.collect{|role|role.id}.join(','))
    end
  }
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_restriction-1.2.4 app/models/product_decorator.rb
spree_restriction-1.2.3 app/models/product_decorator.rb