spree_restriction 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ \#*
2
+ *~
3
+ .#*
4
+ .DS_Store
5
+ .idea
6
+ .project
7
+ coverage
8
+ Gemfile.lock
9
+ tmp
10
+ nbproject
11
+ pkg
12
+ *.swp
13
+ spec/dummy
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.rvmrc ADDED
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p194@spree_restriction"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.15.8 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ if [[ $- == *i* ]] # check for interactive shells
29
+ then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
30
+ else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
31
+ fi
32
+ else
33
+ # If the environment file has not yet been created, use the RVM CLI to select.
34
+ rvm --create use "$environment_id" || {
35
+ echo "Failed to create RVM environment '${environment_id}'."
36
+ return 1
37
+ }
38
+ fi
39
+
40
+ # If you use bundler, this might be useful to you:
41
+ # if [[ -s Gemfile ]] && {
42
+ # ! builtin command -v bundle >/dev/null ||
43
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
44
+ # }
45
+ # then
46
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
47
+ # gem install bundler
48
+ # fi
49
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
50
+ # then
51
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
52
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2012 [name of plugin creator]
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name Spree nor the names of its contributors may be used to
13
+ endorse or promote products derived from this software without specific
14
+ prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ SpreeRestriction
2
+ ================
3
+
4
+ Spree extension for adding visibility restriction by roles to products.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ Add the following line to your Gemfile :
10
+
11
+ gem 'spree_restriction'
12
+
13
+ And then execute the following commands at the root of your application :
14
+
15
+ bundle # to update the installed gems
16
+ rails g spree_restriction:install # to install the extension's migrations and other files into your application
17
+
18
+ Usage
19
+ =====
20
+
21
+ In the admin mode, go to a product and select a role in the Restriction field (at the very bottom of the page). Click update and then logout. Notice that the product is not visible anymore when the required role is not logged in.
22
+
23
+ Testing
24
+ -------
25
+
26
+ I did not implemented any rspec testing yet.
27
+
28
+ Copyright (c) 2012 [name of extension creator], released under the New BSD License
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ require 'spree/core/testing_support/common_rake'
6
+
7
+ RSpec::Core::RakeTask.new
8
+
9
+ task :default => [:spec]
10
+
11
+ desc 'Generates a dummy app for testing'
12
+ task :test_app do
13
+ ENV['LIB_NAME'] = 'spree_restriction'
14
+ Rake::Task['common:test_app'].invoke
15
+ end
data/Versionfile ADDED
@@ -0,0 +1,11 @@
1
+ # This file is used to designate compatibilty with different versions of Spree
2
+ # Please see http://spreecommerce.com/documentation/extensions.html#versionfile for details
3
+
4
+ # Examples
5
+ #
6
+ # '1.2.x' => { :branch => 'master' }
7
+ # '1.1.x' => { :branch => '1-1-stable' }
8
+ # '1.0.x' => { :branch => '1-0-stable' }
9
+ # '0.70.x' => { :branch => '0-70-stable' }
10
+ # '0.40.x' => { :tag => 'v1.0.0', :version => '1.0.0' }
11
+
@@ -0,0 +1,9 @@
1
+ Spree::Admin::ProductsController.class_eval do
2
+ def load_data
3
+ @taxons = Spree::Taxon.order(:name)
4
+ @option_types = Spree::OptionType.order(:name)
5
+ @tax_categories = Spree::TaxCategory.order(:name)
6
+ @shipping_categories = Spree::ShippingCategory.order(:name)
7
+ @roles = Spree::Role.order(:name)
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ Spree::HomeController.class_eval do
2
+ def index
3
+ params[:user_id] = try_spree_current_user.id if try_spree_current_user
4
+ @searcher = Spree::Config.searcher_class.new(params)
5
+ @products = @searcher.retrieve_products
6
+ respond_with(@products)
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ Spree::ProductsController.class_eval do
2
+ def index
3
+ params[:user_id] = try_spree_current_user.id if try_spree_current_user
4
+ @searcher = Spree::Config.searcher_class.new(params)
5
+ @products = @searcher.retrieve_products
6
+ respond_with(@products)
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ Spree::TaxonsController.class_eval do
2
+ def show
3
+ @taxon = Spree::Taxon.find_by_permalink!(params[:id])
4
+ return unless @taxon
5
+
6
+ params[:user_id] = try_spree_current_user.id if try_spree_current_user
7
+ @searcher = Spree::Config.searcher_class.new(params.merge(:taxon => @taxon.id))
8
+ @products = @searcher.retrieve_products
9
+
10
+ respond_with(@taxon)
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ Spree::Admin::ProductsHelper.class_eval do
2
+ def roles_for(product)
3
+ options = @roles.map do |role|
4
+ selected = product.roles.include?(role)
5
+ content_tag(:option,
6
+ :value => role.id,
7
+ :selected => ('selected' if selected)) do
8
+ role.name
9
+ end
10
+ end.join("").html_safe
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ Spree::Core::Search::Base.class_eval do
2
+ def get_base_scope
3
+ roles = @properties[:user_id] ? Spree::User.find(@properties[:user_id]).spree_roles.dup : []
4
+ base_scope = Spree::Product.active
5
+ base_scope = base_scope.by_roles(roles)
6
+ base_scope = base_scope.in_taxon(taxon) unless taxon.blank?
7
+ base_scope = get_products_conditions_for(base_scope, keywords)
8
+ base_scope = base_scope.on_hand unless Spree::Config[:show_zero_stock_products]
9
+ base_scope = add_search_scopes(base_scope)
10
+ base_scope
11
+ end
12
+
13
+ def prepare(params)
14
+ @properties[:taxon] = params[:taxon].blank? ? nil : Spree::Taxon.find(params[:taxon])
15
+ @properties[:keywords] = params[:keywords]
16
+ @properties[:search] = params[:search]
17
+
18
+ per_page = params[:per_page].to_i
19
+ @properties[:per_page] = per_page > 0 ? per_page : Spree::Config[:products_per_page]
20
+ @properties[:page] = (params[:page].to_i <= 0) ? 1 : params[:page].to_i
21
+ @properties[:user_id] = params[:user_id]
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ Spree::Product.class_eval do
2
+ attr_accessible :role_ids
3
+
4
+ has_and_belongs_to_many :roles, :join_table => 'spree_restrictions'
5
+
6
+ scope :by_roles, lambda{ |user_roles|
7
+ if(user_roles.include?(Spree::Role.find_by_name('admin')))
8
+ where('1=1')
9
+ else
10
+ where('spree_products.id IN (
11
+ SELECT p.id FROM spree_products p LEFT JOIN spree_restrictions r ON r.product_id = p.id WHERE r.product_id IS NULL
12
+ )
13
+ OR spree_products.id IN (
14
+ SELECT p.id FROM spree_products p JOIN spree_restrictions r ON r.product_id = p.id WHERE role_id IN (?)
15
+ )', user_roles.collect{|role|role.id}.join(','))
16
+ end
17
+ }
18
+ end
@@ -0,0 +1,3 @@
1
+ Spree::Role.class_eval do
2
+ has_and_belongs_to_many :products, :join_table => 'spree_restrictions'
3
+ end
@@ -0,0 +1,12 @@
1
+ Deface::Override.new(:virtual_path => "spree/admin/products/_form",
2
+ :name => "admin_product_restriction",
3
+ :insert_top => "[data-hook='admin_product_form_additional_fields']",
4
+ :locals => {:stars => 3, :edit_enabled => true},
5
+ :text => '<h2><%= t(:restriction_label) %></h2>
6
+ <div data-hook="admin_product_restrictions">
7
+ <%= f.field_container :restrictions do %>
8
+ <%= f.label :role_ids, t(:restriction_description) %><br />
9
+ <%= f.select :role_ids, roles_for(@product), {}, :class => "select2", :multiple => true %>
10
+ <% end %>
11
+ </div>',
12
+ :disabled => false)
@@ -0,0 +1,3 @@
1
+ en:
2
+ restriction_label: "Restriction"
3
+ restriction_description: "Restrict visibility to these roles"
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Spree::Core::Engine.routes.draw do
2
+ # Add your extension routes here
3
+ end
@@ -0,0 +1,9 @@
1
+ class AddRestrictions < ActiveRecord::Migration
2
+ def change
3
+ create_table :spree_restrictions do |t|
4
+ t.integer :product_id, :references => :spree_products
5
+ t.integer :role_id, :references => :spree_roles
6
+ end
7
+ add_index :spree_restrictions, ["product_id", "role_id"]
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ module SpreeRestriction
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+
5
+ def add_migrations
6
+ run 'bundle exec rake railties:install:migrations FROM=spree_restriction'
7
+ end
8
+
9
+ def run_migrations
10
+ res = ask 'Would you like to run the migrations now? [Y/n]'
11
+ if res == '' || res.downcase == 'y'
12
+ run 'bundle exec rake db:migrate'
13
+ else
14
+ puts 'Skipping rake db:migrate, don\'t forget to run it!'
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module SpreeRestriction
2
+ class Engine < Rails::Engine
3
+ require 'spree/core'
4
+ isolate_namespace Spree
5
+ engine_name 'spree_restriction'
6
+
7
+ config.autoload_paths += %W(#{config.root}/lib)
8
+
9
+ # use rspec for tests
10
+ config.generators do |g|
11
+ g.test_framework :rspec
12
+ end
13
+
14
+ def self.activate
15
+ Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
16
+ Rails.configuration.cache_classes ? require(c) : load(c)
17
+ end
18
+ end
19
+
20
+ config.to_prepare &method(:activate).to_proc
21
+ end
22
+ end
@@ -0,0 +1,2 @@
1
+ require 'spree_core'
2
+ require 'spree_restriction/engine'
data/script/rails ADDED
@@ -0,0 +1,7 @@
1
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
2
+
3
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
4
+ ENGINE_PATH = File.expand_path('../../lib/spree_restriction/engine', __FILE__)
5
+
6
+ require 'rails/all'
7
+ require 'rails/engine/commands'
@@ -0,0 +1,44 @@
1
+ # Configure Rails Environment
2
+ ENV['RAILS_ENV'] = 'test'
3
+
4
+ require File.expand_path('../dummy/config/environment.rb', __FILE__)
5
+
6
+ require 'rspec/rails'
7
+ require 'ffaker'
8
+
9
+ # Requires supporting ruby files with custom matchers and macros, etc,
10
+ # in spec/support/ and its subdirectories.
11
+ Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }
12
+
13
+ # Requires factories defined in spree_core
14
+ require 'spree/core/testing_support/factories'
15
+ require 'spree/core/url_helpers'
16
+
17
+ RSpec.configure do |config|
18
+ config.include FactoryGirl::Syntax::Methods
19
+
20
+ # == URL Helpers
21
+ #
22
+ # Allows access to Spree's routes in specs:
23
+ #
24
+ # visit spree.admin_path
25
+ # current_path.should eql(spree.products_path)
26
+ config.include Spree::Core::UrlHelpers
27
+
28
+ # == Mock Framework
29
+ #
30
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
31
+ #
32
+ # config.mock_with :mocha
33
+ # config.mock_with :flexmock
34
+ # config.mock_with :rr
35
+ config.mock_with :rspec
36
+
37
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
38
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
39
+
40
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
41
+ # examples within a transaction, remove the following line or assign false
42
+ # instead of true.
43
+ config.use_transactional_fixtures = true
44
+ end
Binary file
Binary file
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+ Gem::Specification.new do |s|
3
+ s.platform = Gem::Platform::RUBY
4
+ s.name = 'spree_restriction'
5
+ s.version = '1.2.3'
6
+ s.summary = 'Spree extension for adding visibility restriction by roles to products.'
7
+ s.description = 'Spree extension for adding visibility restriction by roles to products.'
8
+ s.required_ruby_version = '>= 1.8.7'
9
+
10
+ s.author = 'Yanick Landry (Nurun)'
11
+ s.email = 'yanick.landry@nurun.com'
12
+ # s.homepage = 'http://www.spreecommerce.com'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.require_path = 'lib'
17
+ s.requirements << 'none'
18
+
19
+ s.add_dependency 'spree_core', '~> 1.2'
20
+
21
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spree_restriction
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yanick Landry (Nurun)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: spree_core
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.2'
30
+ description: Spree extension for adding visibility restriction by roles to products.
31
+ email: yanick.landry@nurun.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - .gitignore
37
+ - .rspec
38
+ - .rvmrc
39
+ - Gemfile
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - Versionfile
44
+ - app/controllers/admin_products_controller_decorator.rb
45
+ - app/controllers/home_controller_decorator.rb
46
+ - app/controllers/products_controller_decorator.rb
47
+ - app/controllers/taxons_controller_decorator.rb
48
+ - app/helpers/products_helper_decorator.rb
49
+ - app/lib/base_decorator.rb
50
+ - app/models/product_decorator.rb
51
+ - app/models/role_decorator.rb
52
+ - app/overrides/add_restriction_to_admin.rb
53
+ - config/locales/en.yml
54
+ - config/routes.rb
55
+ - db/migrate/20120926201604_add_restrictions.rb
56
+ - lib/generators/spree_restriction/install/install_generator.rb
57
+ - lib/spree_restriction.rb
58
+ - lib/spree_restriction/engine.rb
59
+ - script/rails
60
+ - spec/spec_helper.rb
61
+ - spree_restriction-1.2.1.gem
62
+ - spree_restriction-1.2.2.gem
63
+ - spree_restriction.gemspec
64
+ homepage:
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.8.7
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements:
83
+ - none
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.24
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Spree extension for adding visibility restriction by roles to products.
89
+ test_files:
90
+ - spec/spec_helper.rb