17 Commits
1.6.8 ... 1.6.9

Author SHA1 Message Date
Ryan Bates
38d4654523 releasing 1.6.9 2013-02-04 14:18:38 -08:00
Ryan Bates
3f4ee12025 add gem version badge (thanks Gemfury) 2012-12-11 16:58:20 -08:00
Ryan Bates
4dcd544594 Merge pull request #765 from jonsgreen/issue/cancan_inserting_and_null_687
Issue #687: cancan inserting "AND (NULL)" at the end of sql
2012-10-25 14:25:46 -07:00
jonathangreenberg
f5b3fcd8db Issue #687: cancan inserting "AND (NULL)" at the end of sql
Ensure that empty conditions does not trigger unmergeable conditions
2012-10-24 05:36:41 -04:00
Vasiliy Ermolovich
3b50fedd5d Merge pull request #708 from calebthompson/patch-1
Refold generated Ability comments at 80 characters
2012-10-15 11:09:33 -07:00
Vasiliy Ermolovich
d4be93bc83 add contributing guide
[ci skip]
2012-10-13 21:01:46 +03:00
Vasiliy Ermolovich
9a84277549 show build status fro master branch 2012-10-13 20:57:48 +03:00
Caleb Thompson
857dd075c5 Refold generated Ability comments at 80 characters
A lot of people still fold their code at 80 characters, so it is
nice to have generated code consider this.
2012-08-01 17:44:21 -03:00
Ryan Bates
b4285ae43c Merge pull request #676 from brynary/master
Add Code Climate badge
2012-07-03 10:48:55 -07:00
Ryan Bates
2db73e60c6 Merge pull request #670 from andhapp/fix-issue-664
Namespaced Controllers not building new resource from params(regression 1.6.8)
2012-07-02 13:47:15 -07:00
Ryan Bates
cad4db2d7b Merge pull request #660 from fl00r/master
Segmentation fault on aliasing
2012-07-02 13:36:44 -07:00
Ryan Bates
d20d90d2c2 Merge pull request #655 from DavidMikeSimon/master
Fix for issue #560
2012-07-02 13:27:21 -07:00
Bryan Helmkamp
8b993ee27d Add Code Climate badge 2012-07-02 14:32:04 -03:00
Anuj Dutta
60bc9e98a7 Add code for fixing issue #664 (regression in 1.6.8). 2012-06-29 18:53:16 +01:00
fl00r
925274d29a Fixing Segmentation fault on aliasing 2012-06-23 18:25:13 +04:00
David Mike Simon
b162871c6d Spec to test against nested joins being thrown away ala issue 560 2012-06-19 16:58:15 -07:00
David Mike Simon
cfc355c006 Fix for issue 560 where joins could be thrown away by ActiveRecordAdapter::merge_joins 2012-06-18 19:40:48 -07:00
13 changed files with 101 additions and 16 deletions

View File

@@ -1,3 +1,14 @@
1.6.9 (February 4, 2013)
* fix inserting AND (NULL) to end of SQL queries (thanks jonsgreen) - issue #687
* fix merge_joins for nested association hashes (thanks DavidMikeSimon) - issues #655, #560
* raise error on recursive alias_action (thanks fl00r) - issue #660
* fix namespace controllers not loading params (thanks andhapp) - issues #670, #664
1.6.8 (June 25, 2012) 1.6.8 (June 25, 2012)
* improved support for namespaced controllers and models * improved support for namespaced controllers and models

11
CONTRIBUTING.md Normal file
View File

@@ -0,0 +1,11 @@
### Please read before contributing
1) If you have any questions about CanCan, search the [Wiki](https://github.com/ryanb/cancan/wiki) or use [Stack Overflow](http://stackoverflow.com/questions/tagged/cancan). Do not post questions here.
2) If you find a security bug, **DO NOT** submit an issue here. Please send an e-mail to [ryan@railscasts.com](mailto:ryan@railscasts.com) instead.
3) Do a small search on the issues tracker before submitting your issue to see if it was already reported / fixed. In case it was not, create your report including Rails and CanCan versions. If you are getting exceptions, please include the full backtrace.
That's it! The more information you give, the more easy it becomes for us to track it down and fix it. Ideal scenario would be adding the issue to CanCan test suite or to a sample application.
Thanks!

View File

@@ -1,4 +1,4 @@
= CanCan {<img src="https://secure.travis-ci.org/ryanb/cancan.png" />}[http://travis-ci.org/ryanb/cancan] = CanCan {<img src="https://fury-badge.herokuapp.com/rb/cancan.png" alt="Gem Version" />}[http://badge.fury.io/rb/cancan] {<img src="https://secure.travis-ci.org/ryanb/cancan.png?branch=master" />}[http://travis-ci.org/ryanb/cancan] {<img src="https://codeclimate.com/badge.png" />}[https://codeclimate.com/github/ryanb/cancan]
Wiki[https://github.com/ryanb/cancan/wiki] | RDocs[http://rdoc.info/projects/ryanb/cancan] | Screencast[http://railscasts.com/episodes/192-authorization-with-cancan] Wiki[https://github.com/ryanb/cancan/wiki] | RDocs[http://rdoc.info/projects/ryanb/cancan] | Screencast[http://railscasts.com/episodes/192-authorization-with-cancan]

View File

@@ -1,6 +1,6 @@
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = "cancan" s.name = "cancan"
s.version = "1.6.8" s.version = "1.6.9"
s.author = "Ryan Bates" s.author = "Ryan Bates"
s.email = "ryan@railscasts.com" s.email = "ryan@railscasts.com"
s.homepage = "http://github.com/ryanb/cancan" s.homepage = "http://github.com/ryanb/cancan"

View File

@@ -172,10 +172,16 @@ module CanCan
# This way one can use params[:action] in the controller to determine the permission. # This way one can use params[:action] in the controller to determine the permission.
def alias_action(*args) def alias_action(*args)
target = args.pop[:to] target = args.pop[:to]
validate_target(target)
aliased_actions[target] ||= [] aliased_actions[target] ||= []
aliased_actions[target] += args aliased_actions[target] += args
end end
# User shouldn't specify targets with names of real actions or it will cause Seg fault
def validate_target(target)
raise Error, "You can't specify target (#{target}) as alias because it is real action name" if aliased_actions.values.flatten.include? target
end
# Returns a hash of aliased actions. The key is the target and the value is an array of actions aliasing the key. # Returns a hash of aliased actions. The key is the target and the value is an array of actions aliasing the key.
def aliased_actions def aliased_actions
@aliased_actions ||= default_alias_actions @aliased_actions ||= default_alias_actions

View File

@@ -213,10 +213,15 @@ module CanCan
def resource_params def resource_params
if @options[:class] if @options[:class]
@params[@options[:class].to_s.underscore.gsub('/', '_')] params_key = extract_key(@options[:class])
else return @params[params_key] if @params[params_key]
@params[namespaced_name.to_s.underscore.gsub("/", "_")]
end end
resource_params_by_namespaced_name
end
def resource_params_by_namespaced_name
@params[extract_key(namespaced_name)]
end end
def namespace def namespace
@@ -244,5 +249,11 @@ module CanCan
def new_actions def new_actions
[:new, :create] + [@options[:new]].flatten [:new, :create] + [@options[:new]].flatten
end end
private
def extract_key(value)
value.to_s.underscore.gsub('/', '_')
end
end end
end end

View File

@@ -145,8 +145,8 @@ module CanCan
# Takes two hashes and does a deep merge. # Takes two hashes and does a deep merge.
def merge_joins(base, add) def merge_joins(base, add)
add.each do |name, nested| add.each do |name, nested|
if base[name].is_a?(Hash) && !nested.empty? if base[name].is_a?(Hash)
merge_joins(base[name], nested) merge_joins(base[name], nested) unless nested.empty?
else else
base[name] = nested base[name] = nested
end end

View File

@@ -55,7 +55,8 @@ module CanCan
end end
def unmergeable? def unmergeable?
@conditions.respond_to?(:keys) && (! @conditions.keys.first.kind_of? Symbol) @conditions.respond_to?(:keys) && @conditions.present? &&
(!@conditions.keys.first.kind_of? Symbol)
end end
def associations_hash(conditions = @conditions) def associations_hash(conditions = @conditions)

View File

@@ -11,18 +11,22 @@ class Ability
# can :read, :all # can :read, :all
# end # end
# #
# The first argument to `can` is the action you are giving the user permission to do. # The first argument to `can` is the action you are giving the user
# If you pass :manage it will apply to every action. Other common actions here are # permission to do.
# :read, :create, :update and :destroy. # If you pass :manage it will apply to every action. Other common actions
# here are :read, :create, :update and :destroy.
# #
# The second argument is the resource the user can perform the action on. If you pass # The second argument is the resource the user can perform the action on.
# :all it will apply to every resource. Otherwise pass a Ruby class of the resource. # If you pass :all it will apply to every resource. Otherwise pass a Ruby
# class of the resource.
# #
# The third argument is an optional hash of conditions to further filter the objects. # The third argument is an optional hash of conditions to further filter the
# objects.
# For example, here the user can only update published articles. # For example, here the user can only update published articles.
# #
# can :update, Article, :published => true # can :update, Article, :published => true
# #
# See the wiki for details: https://github.com/ryanb/cancan/wiki/Defining-Abilities # See the wiki for details:
# https://github.com/ryanb/cancan/wiki/Defining-Abilities
end end
end end

View File

@@ -87,6 +87,10 @@ describe CanCan::Ability do
@ability.can?(:increment, 123).should be_true @ability.can?(:increment, 123).should be_true
end end
it "should raise an Error if alias target is an exist action" do
lambda{ @ability.alias_action :show, :to => :show }.should raise_error(CanCan::Error, "You can't specify target (show) as alias because it is real action name")
end
it "should always call block with arguments when passing no arguments to can" do it "should always call block with arguments when passing no arguments to can" do
@ability.can do |action, object_class, object| @ability.can do |action, object_class, object|
action.should == :foo action.should == :foo

View File

@@ -75,13 +75,19 @@ describe CanCan::ControllerResource do
end end
it "should build a new resource for namespaced model with hash if params[:id] is not specified" do it "should build a new resource for namespaced model with hash if params[:id] is not specified" do
project = Sub::Project.create!
@params.merge!(:action => "create", 'sub_project' => {:name => "foobar"}) @params.merge!(:action => "create", 'sub_project' => {:name => "foobar"})
resource = CanCan::ControllerResource.new(@controller, :class => ::Sub::Project) resource = CanCan::ControllerResource.new(@controller, :class => ::Sub::Project)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).name.should == "foobar" @controller.instance_variable_get(:@project).name.should == "foobar"
end end
it "should build a new resource for namespaced controller and namespaced model with hash if params[:id] is not specified" do
@params.merge!(:controller => "Admin::SubProjectsController", :action => "create", 'sub_project' => {:name => "foobar"})
resource = CanCan::ControllerResource.new(@controller, :class => Project)
resource.load_resource
@controller.instance_variable_get(:@sub_project).name.should == "foobar"
end
it "should build a new resource with attributes from current ability" do it "should build a new resource with attributes from current ability" do
@params.merge!(:action => "new") @params.merge!(:action => "new")
@ability.can(:create, Project, :name => "from conditions") @ability.can(:create, Project, :name => "from conditions")

View File

@@ -20,10 +20,12 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
t.boolean "secret" t.boolean "secret"
t.integer "priority" t.integer "priority"
t.integer "category_id" t.integer "category_id"
t.integer "user_id"
end end
model do model do
belongs_to :category belongs_to :category
has_many :comments has_many :comments
belongs_to :user
end end
end end
@@ -37,6 +39,15 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
end end
end end
with_model :user do
table do |t|
end
model do
has_many :articles
end
end
before(:each) do before(:each) do
Article.delete_all Article.delete_all
Comment.delete_all Comment.delete_all
@@ -227,6 +238,21 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
@ability.model_adapter(Article, :read).joins.should == [:project] @ability.model_adapter(Article, :read).joins.should == [:project]
end end
it "should merge nested and non-nested joins" do
@ability.can :read, Article, :project => { :blocked => false }
@ability.can :read, Article, :project => { :comments => { :spam => true } }
@ability.model_adapter(Article, :read).joins.should == [{:project=>[:comments]}]
end
it "should merge :all conditions with other conditions" do
user = User.create!
article = Article.create!(:user => user)
ability = Ability.new(user)
ability.can :manage, :all
ability.can :manage, Article, :user_id => user.id
Article.accessible_by(ability).should == [article]
end
it "should restrict articles given a MetaWhere condition" do it "should restrict articles given a MetaWhere condition" do
@ability.can :read, Article, :priority.lt => 2 @ability.can :read, Article, :priority.lt => 2
article1 = Article.create!(:priority => 1) article1 = Article.create!(:priority => 1)

View File

@@ -44,4 +44,9 @@ describe CanCan::Rule do
@rule.should be_unmergeable @rule.should be_unmergeable
end end
it "should be mergeable if conditions is an empty hash" do
@conditions = {}
@rule.should_not be_unmergeable
end
end end