adding basic ability module
This commit is contained in:
commit
0cfb8c7c41
20
LICENSE
Normal file
20
LICENSE
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
Copyright (c) 2009 Ryan Bates
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
61
README
Normal file
61
README
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
= Render Caching
|
||||||
|
|
||||||
|
Cache render calls in Rails controllers.
|
||||||
|
|
||||||
|
|
||||||
|
== Install
|
||||||
|
|
||||||
|
First install the gem.
|
||||||
|
|
||||||
|
gem install ryanb-render-caching --source=http://gems.github.com
|
||||||
|
|
||||||
|
Then specify it in your Rails config.
|
||||||
|
|
||||||
|
config.gem 'ryanb-render-caching', :lib => 'render_caching', :source => 'http://gems.github.com'
|
||||||
|
|
||||||
|
Rails 2.1 or later required.
|
||||||
|
|
||||||
|
|
||||||
|
== Usage
|
||||||
|
|
||||||
|
This gem adds the render_with_cache method to all controllers. Call
|
||||||
|
this inside of an action to cache the view.
|
||||||
|
|
||||||
|
def show
|
||||||
|
@user = User.find(params[:id])
|
||||||
|
render_with_cache
|
||||||
|
end
|
||||||
|
|
||||||
|
This will cache the full rendered contents into a key matching the URL
|
||||||
|
path (similar to action caching). You can change this key by simply
|
||||||
|
passing any parameter.
|
||||||
|
|
||||||
|
def show
|
||||||
|
@user = User.find(params[:id])
|
||||||
|
render_with_cache @user.cache_key
|
||||||
|
end
|
||||||
|
|
||||||
|
Cache key is a method supplied by Rails. This includes the updated_at
|
||||||
|
time which will give you an auto-expiring cache when the user record is
|
||||||
|
updated.
|
||||||
|
|
||||||
|
You can also supply a block to the render call which will only get
|
||||||
|
executed if there is no cache. Here is a good place to do any custom
|
||||||
|
render calls.
|
||||||
|
|
||||||
|
def show
|
||||||
|
@user = User.find(params[:id])
|
||||||
|
render_with_cache @user.cache_key do
|
||||||
|
render :layout => false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
== Development
|
||||||
|
|
||||||
|
This project can be found on github at the following URL.
|
||||||
|
|
||||||
|
http://github.com/ryanb/render-caching/
|
||||||
|
|
||||||
|
If you would like to contribute to this project, please fork the
|
||||||
|
repository and send me a pull request.
|
11
Rakefile
Normal file
11
Rakefile
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
require 'rubygems'
|
||||||
|
require 'rake'
|
||||||
|
require 'spec/rake/spectask'
|
||||||
|
|
||||||
|
spec_files = Rake::FileList["spec/**/*_spec.rb"]
|
||||||
|
|
||||||
|
desc "Run specs"
|
||||||
|
Spec::Rake::SpecTask.new do |t|
|
||||||
|
t.spec_files = spec_files
|
||||||
|
t.spec_opts = ["-c"]
|
||||||
|
end
|
2
lib/cancan.rb
Normal file
2
lib/cancan.rb
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
$:.unshift(File.dirname(__FILE__))
|
||||||
|
require 'cancan/ability'
|
39
lib/cancan/ability.rb
Normal file
39
lib/cancan/ability.rb
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
module CanCan
|
||||||
|
module Ability
|
||||||
|
def self.included(base)
|
||||||
|
base.extend ClassMethods
|
||||||
|
end
|
||||||
|
|
||||||
|
def can?(action, target)
|
||||||
|
self.class.can_history.reverse.each do |can_action, can_target, can_block|
|
||||||
|
if can_action == action && (can_target == :all || can_target == target || target.kind_of?(can_target))
|
||||||
|
if can_block.nil?
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
if can_target == :all
|
||||||
|
if target.class == Class
|
||||||
|
return can_block.call(target, nil)
|
||||||
|
else
|
||||||
|
return can_block.call(target.class, target)
|
||||||
|
end
|
||||||
|
elsif can_target == target
|
||||||
|
return can_block.call(nil)
|
||||||
|
else
|
||||||
|
return can_block.call(target)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
attr_reader :can_history
|
||||||
|
|
||||||
|
def can(action, target, &block)
|
||||||
|
@can_history ||= []
|
||||||
|
@can_history << [action, target, block]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
40
spec/cancan/ability_spec.rb
Normal file
40
spec/cancan/ability_spec.rb
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
require File.dirname(__FILE__) + '/../spec_helper'
|
||||||
|
|
||||||
|
class Ability
|
||||||
|
include CanCan::Ability
|
||||||
|
can :read, :all
|
||||||
|
can :read, Symbol do |sym|
|
||||||
|
sym
|
||||||
|
end
|
||||||
|
can :preview, :all do |object_class, object|
|
||||||
|
[object_class, object]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe CanCan::Ability do
|
||||||
|
before(:each) do
|
||||||
|
@ability = Ability.new
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be able to :read anything" do
|
||||||
|
@ability.can?(:read, String).should be_true
|
||||||
|
@ability.can?(:read, 123).should be_true
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not have permission to do something it doesn't know about" do
|
||||||
|
@ability.can?(:foodfight, String).should be_false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return what block returns on a can call" do
|
||||||
|
@ability.can?(:read, Symbol).should be_nil
|
||||||
|
@ability.can?(:read, :some_symbol).should == :some_symbol
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should pass class with object if :all objects are accepted" do
|
||||||
|
@ability.can?(:preview, 123).should == [Fixnum, 123]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should pass class with no object if :all objects are accepted and class is passed directly" do
|
||||||
|
@ability.can?(:preview, Hash).should == [Hash, nil]
|
||||||
|
end
|
||||||
|
end
|
11
spec/spec_helper.rb
Normal file
11
spec/spec_helper.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
require 'rubygems'
|
||||||
|
require 'spec'
|
||||||
|
require 'active_support'
|
||||||
|
require 'active_record'
|
||||||
|
require 'action_controller'
|
||||||
|
require 'action_view'
|
||||||
|
require File.dirname(__FILE__) + '/../lib/cancan.rb'
|
||||||
|
|
||||||
|
Spec::Runner.configure do |config|
|
||||||
|
config.mock_with :rr
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user