cancan-rest-links 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format nested --color
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = cancan-rest-links
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gem|
4
+ gem.name = "cancan-rest-links"
5
+ gem.summary = %Q{Rest link helpers for CanCan}
6
+ gem.description = %Q{Guard your links with permissions}
7
+ gem.email = "kmandrup@gmail.com"
8
+ gem.homepage = "http://github.com/kristianmandrup/cancan-rest-links"
9
+ gem.authors = ["Kristian Mandrup"]
10
+ gem.add_development_dependency "rspec", "~> 2.0.0"
11
+ gem.add_dependency 'cancan', "~> 1.3"
12
+ gem.add_dependency 'require_all', "~> 1.1"
13
+ gem.add_dependency 'sugar-high', "~> 0.1"
14
+ # gem.add_dependency 'rails3_plugin_toolbox', "~> 0.3"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,66 @@
1
+ module CanCan::Link
2
+ module Rest
3
+ def index_link(object, label = nil)
4
+ label ||= auth_labels[:index]
5
+ obj = index_obj(object)
6
+ path = send :"#{obj}_path"
7
+ link = link_to(label, path) if can?(:read, object)
8
+ end
9
+
10
+ def index_obj(obj)
11
+ o = case obj
12
+ when Array
13
+ obj.first.class
14
+ when Class
15
+ obj
16
+ else
17
+ obj.class
18
+ end
19
+ o.name.pluralize.downcase
20
+ end
21
+
22
+ def create_link(object, label = nil)
23
+ label ||= auth_labels[:new]
24
+ link_to(content, ) if can?(:create, object)
25
+ # path = send :"new_#{object.class.to_s.downcase}_path"
26
+ link = link_to(label, [:new, object.name.underscore.to_sym]) if can?(:create, object)
27
+ end
28
+
29
+ def edit_link(object, label = nil)
30
+ label ||= auth_labels[:edit]
31
+ link_to(label, [:edit, object]) if can?(:edit, object)
32
+ end
33
+
34
+ def delete_link(object, options = nil)
35
+ options ||= {:label => auth_labels[:delete], :confirm => auth_labels[:confirm]}
36
+ case options
37
+ when String
38
+ label = options
39
+ when Hash
40
+ label = options[:label]
41
+ confirm_msg = options[:confirm]
42
+ when Array
43
+ label = options[0]
44
+ confirm_msg = options.size > 1 ? options[1] : auth_labels[:confirm]
45
+ end
46
+ link_to(label, object, :method => :delete, :confirm => confirm_msg) if can?(:destroy, object)
47
+ end
48
+
49
+ def show_link(object, label = nil)
50
+ label ||= auth_labels[:show]
51
+ link_to(label, object) if can?(:read, object)
52
+ end
53
+
54
+ LINKS = {
55
+ :index => :list,
56
+ :create => :new,
57
+ :delete => :destroy,
58
+ :edit => :update,
59
+ :show => :read
60
+ }
61
+
62
+ # alias_method :list_link, :index_link and so on...
63
+ multi_alias LINKS.merge(:_after_ => :link)
64
+ end
65
+ end
66
+
@@ -0,0 +1,3 @@
1
+ module CanCan
2
+ modules :link
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_support/railtie'
2
+ require 'rails3_plugin_toolbox'
3
+
4
+ Rails3::PluginExtender.new do
5
+ # extend action_view with methods from some modules
6
+ extend_rails :view do
7
+ extend_with CanCan::Link::Rest
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ require 'require_all'
2
+ require 'sugar-high/module'
3
+ require 'cancan-rest-links/namespaces'
4
+ require_all File.dirname(__FILE__) + '/cancan-rest-links/helpers'
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ # extend_view_with CanCan::Link::Rest
4
+ require 'cancan-rest-links/rails/configure'
5
+
6
+ class Post
7
+ end
8
+
9
+ describe AuthAssistant::Link::Rest do
10
+ describe '#index_link' do
11
+ it "should create an index link" do
12
+ with_engine do |e, view|
13
+ label = 'index'
14
+ path = 'post/index'
15
+ post = Post.new
16
+
17
+ view.stubs(:auth_labels).returns(:index => label)
18
+
19
+ view.stubs(:posts_path).returns path
20
+ view.stubs(:can?).returns true
21
+ view.stubs(:link_to).with(label, path).returns 'it works'
22
+
23
+ res = e.run_template_locals :post => post, :name => label do
24
+ %{<%= index_link(post, name) %> }
25
+ end
26
+ res.should match /it works/
27
+ end
28
+ end
29
+ end
30
+
31
+ describe '#create_link' do
32
+ it "should create a create link" do
33
+ with_engine do |e, view|
34
+ label = 'create'
35
+ path = 'post/create'
36
+ post = Post.new
37
+
38
+ view.stubs(:auth_labels).returns(:index => label)
39
+
40
+ view.stubs(:new_post_path).returns path
41
+ view.stubs(:can?).returns true
42
+ view.stubs(:link_to).with(label, path).returns 'it works'
43
+
44
+ res = e.run_template_locals :post => post, :name => label do
45
+ %{<%= create_link(post, name) %> }
46
+ end
47
+ res.should match /it works/
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '#edit_link' do
53
+ it "should create a create link" do
54
+ with_engine do |e, view|
55
+ label = 'create'
56
+ path = 'post/create'
57
+ post = Post.new
58
+
59
+ view.stubs(:auth_labels).returns(:index => label)
60
+
61
+ view.stubs(:can?).returns true
62
+ view.stubs(:link_to).with(label, [:edit, post]).returns 'it works'
63
+
64
+ res = e.run_template_locals :post => post, :name => label do
65
+ %{<%= edit_link(post, name) %> }
66
+ end
67
+ res.should match /it works/
68
+ end
69
+ end
70
+ end
71
+
72
+ describe '#delete_link' do
73
+ it "should create a delete link" do
74
+ with_engine do |e, view|
75
+ label = 'delete'
76
+ path = 'post/delete'
77
+ post = Post.new
78
+
79
+ view.stubs(:auth_labels).returns(:index => label)
80
+
81
+ view.stubs(:can?).returns true
82
+ view.stubs(:link_to).returns 'it works'
83
+
84
+ res = e.run_template_locals :post => post, :name => label do
85
+ %{<%= delete_link(post, name) %> }
86
+ end
87
+ res.should match /it works/
88
+ end
89
+ end
90
+ end
91
+
92
+
93
+ describe '#show_link' do
94
+ it "should create a show link" do
95
+ with_engine do |e, view|
96
+ label = 'show'
97
+ path = 'post/show'
98
+ post = Post.new
99
+
100
+ view.stubs(:auth_labels).returns(:index => label)
101
+ view.stubs(:can?).returns true
102
+ view.stubs(:link_to).with(label, post).returns 'it works'
103
+
104
+ res = e.run_template_locals :post => post, :name => label do
105
+ %{<%= show_link(post, name) %> }
106
+ end
107
+ res.should match /it works/
108
+ end
109
+ end
110
+ end
111
+ end
112
+
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+ require 'cancan-rest-links/rails/configure'
3
+
4
+ describe 'CanCan REST Links rails View extension' do
5
+ it "should extend Action View with REST Link helpers" do
6
+ after_init :view do
7
+ :view.should be_extended_with CanCan::Links::Rest
8
+ end
9
+
10
+ init_app_railties :minimal, :view
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ require 'rspec'
2
+ require 'rspec/autorun'
3
+ require 'cancan-rest-links'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :mocha
7
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cancan-rest-links
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Kristian Mandrup
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-17 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 0
31
+ - 0
32
+ version: 2.0.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: cancan
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 1
45
+ - 3
46
+ version: "1.3"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: require_all
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 1
60
+ version: "1.1"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: sugar-high
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ - 1
74
+ version: "0.1"
75
+ type: :runtime
76
+ version_requirements: *id004
77
+ description: Guard your links with permissions
78
+ email: kmandrup@gmail.com
79
+ executables: []
80
+
81
+ extensions: []
82
+
83
+ extra_rdoc_files:
84
+ - LICENSE
85
+ - README.rdoc
86
+ files:
87
+ - .document
88
+ - .gitignore
89
+ - .rspec
90
+ - LICENSE
91
+ - README.rdoc
92
+ - Rakefile
93
+ - VERSION
94
+ - lib/cancan-rest-links.rb
95
+ - lib/cancan-rest-links/helpers/rest_links.rb
96
+ - lib/cancan-rest-links/namespaces.rb
97
+ - lib/cancan-rest-links/rails/configure.rb
98
+ - spec/cancan-rest-links/link_helpers_spec.rb
99
+ - spec/cancan-rest-links/rails/configure_spec.rb
100
+ - spec/spec_helper.rb
101
+ has_rdoc: true
102
+ homepage: http://github.com/kristianmandrup/cancan-rest-links
103
+ licenses: []
104
+
105
+ post_install_message:
106
+ rdoc_options:
107
+ - --charset=UTF-8
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ requirements: []
127
+
128
+ rubyforge_project:
129
+ rubygems_version: 1.3.7
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: Rest link helpers for CanCan
133
+ test_files:
134
+ - spec/cancan-rest-links/link_helpers_spec.rb
135
+ - spec/cancan-rest-links/rails/configure_spec.rb
136
+ - spec/spec_helper.rb