Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: RolifyCommunity/rolify
base: 967504f555
...
head fork: RolifyCommunity/rolify
compare: d1c36552df
  • 5 commits
  • 4 files changed
  • 0 commit comments
  • 2 contributors
Commits on Jul 12, 2012
@demental demental spec with new instance 66cb440
@demental demental indentation fix c135ac2
@demental demental implementing has_role? for new_record 0ddda9a
Commits on Jul 31, 2012
@EppO EppO Merge pull request #90 from demental/add_role_to_new_instance
has_role? usable with new instance
a893558
@EppO EppO Mongoid will support :before_add, after_add, :before_remove, :after_r…
…emove callbacks only in 3.1 release.

triggering these callbacks only for ActiveRecord for now
closes #91
d1c3655
View
2  lib/rolify.rb
@@ -19,7 +19,7 @@ def rolify(options = {})
rolify_options = { :class_name => options[:role_cname].camelize }
rolify_options.merge!({ :join_table => "#{self.to_s.tableize}_#{options[:role_cname].tableize}" }) if Rolify.orm == "active_record"
- rolify_options.merge!(options.select{ |k,v| [:before_add, :after_add, :before_remove, :after_remove].include? k.to_sym })
+ rolify_options.merge!(options.select{ |k,v| [:before_add, :after_add, :before_remove, :after_remove].include? k.to_sym }) if Rolify.orm == "active_record"
has_and_belongs_to_many :roles, rolify_options
View
6 lib/rolify/role.rb
@@ -23,7 +23,11 @@ def add_role(role_name, resource = nil)
deprecate :has_role, :add_role
def has_role?(role_name, resource = nil)
- self.class.adapter.where(self.roles, :name => role_name, :resource => resource).size > 0
+ if new_record?
+ self.roles.detect { |r| r.name == role_name.to_s && (r.resource == resource || resource.nil?) }.present?
+ else
+ self.class.adapter.where(self.roles, :name => role_name, :resource => resource).size > 0
+ end
end
def has_all_roles?(*args)
View
2  spec/rolify/shared_examples/shared_examples_for_callbacks.rb
@@ -9,7 +9,7 @@
@user.roles.destroy_all
end
- describe "rolify association callbacks" do
+ describe "rolify association callbacks", :if => (Rolify.orm == "active_record") do
describe "before_add" do
it "should receive callback" do
user_class.rolify :before_add => :role_callback, :role_cname => role_class.to_s
View
50 spec/rolify/shared_examples/shared_examples_for_roles.rb
@@ -48,31 +48,41 @@
it { should_not respond_to(:is_moderator_of?) }
describe "#has_role" do
- it_should_behave_like "#add_role_examples", "String", :to_s
- it_should_behave_like "#add_role_examples", "Symbol", :to_sym
- end
+ it_should_behave_like "#add_role_examples", "String", :to_s
+ it_should_behave_like "#add_role_examples", "Symbol", :to_sym
+ end
- describe "#has_role?" do
- it_should_behave_like "#has_role?_examples", "String", :to_s
- it_should_behave_like "#has_role?_examples", "Symbol", :to_sym
- end
+ describe "#has_role?" do
+ it_should_behave_like "#has_role?_examples", "String", :to_s
+ it_should_behave_like "#has_role?_examples", "Symbol", :to_sym
+ end
- describe "#has_all_roles?" do
- it_should_behave_like "#has_all_roles?_examples", "String", :to_s
- it_should_behave_like "#has_all_roles?_examples", "Symbol", :to_sym
- end
+ describe "#has_all_roles?" do
+ it_should_behave_like "#has_all_roles?_examples", "String", :to_s
+ it_should_behave_like "#has_all_roles?_examples", "Symbol", :to_sym
+ end
- describe "#has_any_role?" do
- it_should_behave_like "#has_any_role?_examples", "String", :to_s
- it_should_behave_like "#has_any_role?_examples", "Symbol", :to_sym
- end
+ describe "#has_any_role?" do
+ it_should_behave_like "#has_any_role?_examples", "String", :to_s
+ it_should_behave_like "#has_any_role?_examples", "Symbol", :to_sym
+ end
- describe "#has_no_role" do
- it_should_behave_like "#remove_role_examples", "String", :to_s
- it_should_behave_like "#remove_role_examples", "Symbol", :to_sym
- end
+ describe "#has_no_role" do
+ it_should_behave_like "#remove_role_examples", "String", :to_s
+ it_should_behave_like "#remove_role_examples", "Symbol", :to_sym
+ end
end
-
+ context "with new instance" do
+ let(:user) { user_class.new }
+ before do
+ user.add_role :admin
+ user.add_role :moderator, Forum.first
+ end
+ subject { user }
+ it { should have_role :admin }
+ it { should have_role :moderator, Forum.first }
+
+ end
context "on the Class level ", :scope => :mixed do
it_should_behave_like :finders, "String", :to_s
it_should_behave_like :finders, "Symbol", :to_sym

No commit comments for this range

Something went wrong with that request. Please try again.