cancan-rest-links 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -11,6 +11,16 @@ Note: The link helpers require a hash called #auth_labels, which is used to look
11
11
 
12
12
  An example of this functionality can be found in the [Cream](http://github.com/kristianmandrup/cream) framework where this gem is used.
13
13
 
14
+ ## Rails 3 usage
15
+
16
+ See the configure_spec in the /specs folder.
17
+
18
+ <code>
19
+ require 'cancan-rest-links/rails/configure'
20
+ </code>
21
+
22
+
23
+
14
24
  ## Note on Patches/Pull Requests
15
25
 
16
26
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.1.6
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cancan-rest-links}
8
- s.version = "0.1.5"
8
+ s.version = "0.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
@@ -1,37 +1,26 @@
1
1
  module CanCan::Link
2
2
  module Rest
3
3
  def index_link(object, label = nil)
4
- label ||= auth_labels[:index]
4
+ label ||= index_label
5
5
  obj = index_obj(object)
6
6
  path = send :"#{obj}_path"
7
7
  link = link_to(label, path) if can?(:read, object)
8
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
-
9
+
22
10
  def create_link(object, label = nil)
23
- label ||= auth_labels[:new]
24
- path = send :"new_#{object.class.to_s.downcase}_path"
11
+ label ||= new_label
12
+ obj_name = object_class(object).to_s.downcase
13
+ path = send :"new_#{obj_name}_path"
25
14
  link = link_to(label, path) if can?(:create, object)
26
15
  end
27
16
 
28
17
  def edit_link(object, label = nil)
29
- label ||= auth_labels[:edit]
18
+ label ||= edit_label
30
19
  link_to(label, [:edit, object]) if can?(:edit, object)
31
20
  end
32
21
 
33
- def delete_link(object, options = nil)
34
- options ||= {:label => auth_labels[:delete], :confirm => auth_labels[:confirm]}
22
+ def delete_link(object, options = nil)
23
+ options ||= {:label => delete_label, :confirm => confirm_label}
35
24
  case options
36
25
  when String
37
26
  label = options
@@ -40,13 +29,13 @@ module CanCan::Link
40
29
  confirm_msg = options[:confirm]
41
30
  when Array
42
31
  label = options[0]
43
- confirm_msg = options.size > 1 ? options[1] : auth_labels[:confirm]
32
+ confirm_msg = options.size > 1 ? options[1] : confirm_label
44
33
  end
45
34
  link_to(label, object, :method => :delete, :confirm => confirm_msg) if can?(:destroy, object)
46
35
  end
47
36
 
48
- def show_link(object, label = nil)
49
- label ||= auth_labels[:show]
37
+ def show_link(object, label = nil)
38
+ label ||= show_label
50
39
  link_to(label, object) if can?(:read, object)
51
40
  end
52
41
 
@@ -60,6 +49,52 @@ module CanCan::Link
60
49
 
61
50
  # alias_method :list_link, :index_link and so on...
62
51
  multi_alias LINKS.merge(:_after_ => :link)
52
+
53
+ protected
54
+
55
+ def index_label
56
+ get_label :index
57
+ end
58
+
59
+ def new_label
60
+ get_label :new
61
+ end
62
+
63
+ def edit_label
64
+ get_label :edit
65
+ end
66
+
67
+ def show_label
68
+ get_label :show
69
+ end
70
+
71
+ def delete_label
72
+ get_label :delete
73
+ end
74
+
75
+ def confirm_label
76
+ get_label :confirm
77
+ end
78
+
79
+ def get_label key
80
+ raise 'the method #auth_labels must be available on the View' if !respond_to? :auth_labels
81
+ auth_labels[key]
82
+ end
83
+
84
+ def index_obj(obj)
85
+ object_class(obj).name.pluralize.downcase
86
+ end
87
+
88
+ def object_class obj
89
+ case obj
90
+ when Array
91
+ obj.first.class
92
+ when Class
93
+ obj
94
+ else
95
+ obj.class
96
+ end
97
+ end
63
98
  end
64
99
  end
65
100
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 5
9
- version: 0.1.5
8
+ - 6
9
+ version: 0.1.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup