Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecation warning on recent Spree versions #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
appraise "spree-3-2" do
gem "spree", "~> 3.2.0"
gem "spree_auth_devise", "~> 3.2.0"
end

appraise "spree-3-3" do
gem "spree", "~> 3.3.0"
gem "spree_auth_devise", "~> 3.3.0"
end

appraise "spree-3-4" do
gem "spree", '~> 3.4.0'
gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: 'master'
end

appraise 'spree-3-5' do
gem 'spree', '~> 3.5.0'
gem 'spree_auth_devise', '~> 3.3.0'
appraise 'spree-3-7' do
gem 'spree', '~> 3.7.0'
gem 'spree_backend', '~> 3.7.0'
gem 'spree_auth_devise', '~> 3.5.0'
end

appraise 'spree-3-6' do
gem 'spree', '~> 3.6.0'
gem 'spree_auth_devise', '~> 3.3.0'
appraise 'spree-4-0' do
gem 'spree', '~> 4.0.0'
gem 'spree_backend', '~> 4.0.0'
gem 'spree_auth_devise'
end

appraise 'spree-3-7' do
gem 'spree', '~> 3.7.0'
gem 'spree_auth_devise', '~> 3.5.0'
appraise 'spree-4-1' do
gem 'spree', '~> 4.1.0'
gem 'spree_backend', '~> 4.1.0'
gem 'spree_auth_devise'
end

appraise 'spree-master' do
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ task default: [:spec]
desc 'Generates a dummy app for testing'
task :test_app do
ENV['LIB_NAME'] = 'spree_admin_roles_and_access'
Rake::Task['extension:test_app'].invoke
Rake::Task['extension:test_app'].invoke('Spree::User')
end
27 changes: 0 additions & 27 deletions app/controllers/spree/admin/base_controller_decorator.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module SpreeAdminRolesAndAccess
module Spree
module Admin
module BaseControllerDecorator
def authorize_admin
begin
if params[:id]
record = model_class.where(PARAM_ATTRIBUTE[controller_name] => params[:id]).first
elsif new_action?
record = model_class.new
else
record = model_class
end
raise if record.blank?
rescue
record = "#{params[:controller]}"
end
authorize! :admin, record
authorize_with_attributes! params[:action].to_sym, record, params[controller_name.singularize]
end

private

def unauthorized
redirect_unauthorized_access
end

def new_action?
NEW_ACTIONS.include?(params[:action].to_sym)
end
end
end
end
end

::Spree::Admin::BaseController.prepend SpreeAdminRolesAndAccess::Spree::Admin::BaseControllerDecorator
28 changes: 0 additions & 28 deletions app/models/spree/ability_decorator.rb

This file was deleted.

18 changes: 0 additions & 18 deletions app/models/spree/role_decorator.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/models/spree/user_decorator.rb

This file was deleted.

31 changes: 31 additions & 0 deletions app/models/spree_admin_roles_and_access/spree/ability_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module SpreeAdminRolesAndAccess
module Spree
module AbilityDecorator
def initialize(user)
self.clear_aliased_actions

alias_action :edit, to: :update
alias_action :new, to: :create
alias_action :new_action, to: :create
alias_action :show, to: :read
alias_action :index, to: :read
alias_action :delete, to: :destroy

user ||= ::Spree.user_class.new

user_roles(user).map(&:permissions).flatten.uniq.map { |permission| permission.ability(self, user) }

::Spree::Ability.abilities.each do |clazz|
ability = clazz.send(:new, user)
@rules = rules + ability.send(:rules)
end
end

def user_roles(user)
(roles = user.roles.includes(:permissions)).empty? ? ::Spree::Role.default_role.includes(:permissions) : roles
end
end
end
end

::Spree::Ability.prepend SpreeAdminRolesAndAccess::Spree::AbilityDecorator
25 changes: 25 additions & 0 deletions app/models/spree_admin_roles_and_access/spree/role_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module SpreeAdminRolesAndAccess
module Spree
module RoleDecorator
def self.prepended(base)
base.has_many :roles_permission_sets, dependent: :destroy
base.has_many :permission_sets, through: :roles_permission_sets
base.has_many :permissions, through: :permission_sets

# DEPRECATED: Use permission sets instead. Only here for aiding migration for existing users
base.has_and_belongs_to_many :legacy_permissions, join_table: 'spree_roles_permissions', class_name: 'Spree::Permission'

base.validates :name, uniqueness: true, allow_blank: true
base.validates :permission_sets, length: { minimum: 1, too_short: :atleast_one_permission_set_is_required }, on: :update

base.scope :default_role, lambda { where(is_default: true) }
end

def has_permission?(permission_title)
permissions.pluck(:title).include?(permission_title)
end
end
end
end

::Spree::Role.prepend SpreeAdminRolesAndAccess::Spree::RoleDecorator
11 changes: 11 additions & 0 deletions app/models/spree_admin_roles_and_access/spree/user_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module SpreeAdminRolesAndAccess
module Spree
module UserDecorator
def self.prepended(base)
base.alias_attribute :roles, :spree_roles
end
end
end
end

::Spree::User.prepend SpreeAdminRolesAndAccess::Spree::UserDecorator
Loading