action_links 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +0 -5
- data/action_links.gemspec +1 -3
- data/lib/action_links/engine.rb +7 -2
- data/lib/action_links/helpers.rb +27 -0
- data/lib/action_links/link.rb +64 -0
- data/lib/action_links/list.rb +67 -0
- data/lib/action_links/version.rb +1 -1
- data/lib/action_links.rb +2 -75
- metadata +6 -3
data/README.rdoc
CHANGED
@@ -14,11 +14,6 @@ Add the following to your Gemfile
|
|
14
14
|
From the command line
|
15
15
|
|
16
16
|
> bundle install
|
17
|
-
|
18
|
-
|
19
|
-
Appdoc uses {declarative_autorization}[https://github.com/stffn/declarative_authorization] for role based permissions. Otherwise you will have to manually implement the method
|
20
|
-
|
21
|
-
permitted_to? :show, @user
|
22
17
|
|
23
18
|
|
24
19
|
== Usage
|
data/action_links.gemspec
CHANGED
@@ -7,14 +7,12 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = ActionLinks::VERSION
|
8
8
|
s.authors = ["Adam Crownoble", "Ryan Hall"]
|
9
9
|
s.email = ["adam@obledesign.com"]
|
10
|
-
s.homepage = "https://github.com/
|
10
|
+
s.homepage = "https://github.com/biola/action_links"
|
11
11
|
s.summary = %q{Quick and painless action links}
|
12
12
|
s.description = %q{Automatically includes action links (show, edit, destroy) based on the current page and user roles/permissions}
|
13
13
|
|
14
14
|
s.rubyforge_project = "action_links"
|
15
15
|
|
16
16
|
s.files = `git ls-files`.split("\n")
|
17
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
17
|
s.require_paths = ["lib"]
|
20
18
|
end
|
data/lib/action_links/engine.rb
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
-
require 'action_links'
|
2
1
|
require 'rails'
|
2
|
+
require 'action_links/list'
|
3
|
+
require 'action_links/link'
|
4
|
+
require 'action_links/helpers'
|
3
5
|
|
4
6
|
module ActionLinks
|
5
|
-
class
|
7
|
+
class Railtie < Rails::Railtie
|
8
|
+
initializer "action_links.helpers" do
|
9
|
+
ActionView::Base.send :include, ActionLinks::Helpers
|
10
|
+
end
|
6
11
|
end
|
7
12
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ActionLinks
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
def action_links(object_or_array, options={}, &block)
|
5
|
+
list = ActionLinks::List.new(object_or_array, options)
|
6
|
+
|
7
|
+
links = list.links.map { |link|
|
8
|
+
unless current_page?(link.url_array) && !link.destroy?
|
9
|
+
if permitted_to?(link.action, link.object)
|
10
|
+
link_to(link.title, link.url_array, link.html_options)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
}
|
14
|
+
|
15
|
+
links << capture(&block) if block_given?
|
16
|
+
|
17
|
+
links.compact!
|
18
|
+
|
19
|
+
content_tag(list.options[:list_wrapper], :class=>:actions) do
|
20
|
+
links.map{ |link|
|
21
|
+
content_tag(list.options[:link_wrapper], link, :class=>list.options[:link_wrapper_class])
|
22
|
+
}.join("\n").html_safe
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module ActionLinks
|
2
|
+
class Link
|
3
|
+
attr_accessor :action
|
4
|
+
attr_accessor :object_array
|
5
|
+
attr_accessor :options
|
6
|
+
|
7
|
+
def initialize(action, object_array, options={})
|
8
|
+
@action = action.to_s.to_sym
|
9
|
+
@object_array = Array(object_array)
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
|
13
|
+
def object
|
14
|
+
@object ||= object_array.last
|
15
|
+
end
|
16
|
+
|
17
|
+
def object_name
|
18
|
+
@object_name ||= object.class.model_name.human
|
19
|
+
end
|
20
|
+
|
21
|
+
def title
|
22
|
+
@title ||= I18n.t(action, :scope=>'action_links', :default=>action.to_s.humanize)
|
23
|
+
end
|
24
|
+
|
25
|
+
def url_array
|
26
|
+
@url_array ||= IMPLICIT_ROUTE_ACTIONS.include?(action) ? object_array : [action, *object_array]
|
27
|
+
end
|
28
|
+
|
29
|
+
def show?
|
30
|
+
action == :show
|
31
|
+
end
|
32
|
+
|
33
|
+
def edit?
|
34
|
+
action == :edit
|
35
|
+
end
|
36
|
+
|
37
|
+
def destroy?
|
38
|
+
action == :destroy
|
39
|
+
end
|
40
|
+
|
41
|
+
def remote?
|
42
|
+
options[:remote].include? action
|
43
|
+
end
|
44
|
+
|
45
|
+
def html_options
|
46
|
+
return @html_options unless @html_options.nil?
|
47
|
+
|
48
|
+
@html_options = { :class=>action.to_s.parameterize, :title=>"#{title} #{object_name}" }
|
49
|
+
@html_options[:remote] = true if remote?
|
50
|
+
|
51
|
+
if action == :destroy
|
52
|
+
@html_options.merge! :method=>:delete, :confirm=>I18n.t(:confirm_delete, :scope=>'action_links', :default=>'Are you sure?')
|
53
|
+
end
|
54
|
+
|
55
|
+
@html_options
|
56
|
+
end
|
57
|
+
|
58
|
+
def wrapper_tag(&block)
|
59
|
+
content_tag(options[:link_wrapper], :class=>options[:link_wrapper_class]) do
|
60
|
+
yield
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module ActionLinks
|
2
|
+
|
3
|
+
IMPLICIT_ROUTE_ACTIONS = [:show, :destroy]
|
4
|
+
|
5
|
+
DEFAULT_OPTIONS = {
|
6
|
+
:actions => [:show, :edit, :destroy],
|
7
|
+
:include => [],
|
8
|
+
:exclude => [],
|
9
|
+
:controller_name => nil,
|
10
|
+
:remote => [],
|
11
|
+
:list_wrapper => :ul,
|
12
|
+
:list_wrapper_class => :actions,
|
13
|
+
:link_wrapper => :li,
|
14
|
+
:link_wrapper_class => nil
|
15
|
+
}
|
16
|
+
|
17
|
+
class List
|
18
|
+
attr_accessor :object_array
|
19
|
+
|
20
|
+
def initialize(object_or_array, options={})
|
21
|
+
@object_array = Array(object_or_array)
|
22
|
+
self.options = options
|
23
|
+
end
|
24
|
+
|
25
|
+
def options
|
26
|
+
@options ||= []
|
27
|
+
end
|
28
|
+
|
29
|
+
def options=(options)
|
30
|
+
@options = options
|
31
|
+
@options.reverse_merge! DEFAULT_OPTIONS
|
32
|
+
@options.each { |key,val| @options[key] = Array(val) if DEFAULT_OPTIONS[key].is_a? Array }
|
33
|
+
@options[:controller_name] ||= [*namespaces, object.class.model_name.plural].compact.join('/')
|
34
|
+
end
|
35
|
+
|
36
|
+
def actions
|
37
|
+
@actions ||= (options[:actions] - options[:exclude]) + options[:include]
|
38
|
+
end
|
39
|
+
|
40
|
+
def object
|
41
|
+
@object ||= object_array.last
|
42
|
+
end
|
43
|
+
|
44
|
+
def namespaces
|
45
|
+
@namespaces ||= object_array.select{ |obj| obj.is_a? Symbol }
|
46
|
+
end
|
47
|
+
|
48
|
+
def controller
|
49
|
+
@controller ||= "#{options[:controller_name]}_controller".classify.constantize.new
|
50
|
+
end
|
51
|
+
|
52
|
+
def links
|
53
|
+
@links ||= actions.map{ |action|
|
54
|
+
if controller.respond_to?(action)
|
55
|
+
Link.new(action, object_array, options)
|
56
|
+
end
|
57
|
+
}.compact
|
58
|
+
end
|
59
|
+
|
60
|
+
def wrapper_tag(&block)
|
61
|
+
content_tag(options[:list_wrapper], :class=>:actions) do
|
62
|
+
yield
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
data/lib/action_links/version.rb
CHANGED
data/lib/action_links.rb
CHANGED
@@ -1,78 +1,5 @@
|
|
1
|
-
require
|
1
|
+
require 'action_links/version'
|
2
|
+
require 'action_links/engine' if defined?(Rails)
|
2
3
|
|
3
4
|
module ActionLinks
|
4
|
-
require 'action_links/engine' if defined?(Rails)
|
5
|
-
|
6
|
-
# for easy configuration
|
7
|
-
def self.config
|
8
|
-
yield self
|
9
|
-
end
|
10
|
-
|
11
|
-
|
12
|
-
# A helper that renders the action links.
|
13
|
-
# Automatically renders :show, :edit, and :destroy if user has permissions to those actions
|
14
|
-
#
|
15
|
-
# <%= action_links @articles %>
|
16
|
-
#
|
17
|
-
def action_links(object_or_array, options={}, &block)
|
18
|
-
implicit_route_actions = [:show, :destroy]
|
19
|
-
|
20
|
-
# TODO: move default actions into a config file
|
21
|
-
defaults = {
|
22
|
-
:actions => [:show, :edit, :destroy],
|
23
|
-
:include => [],
|
24
|
-
:exclude => [],
|
25
|
-
:controller_name => nil,
|
26
|
-
:remote => [],
|
27
|
-
:list_wrapper => :ul,
|
28
|
-
:list_wrapper_class => :actions,
|
29
|
-
:link_wrapper => :li,
|
30
|
-
:link_wrapper_class => nil
|
31
|
-
}
|
32
|
-
|
33
|
-
options.reverse_merge! defaults
|
34
|
-
options.each { |key,val| options[key] = Array(val) if defaults[key].is_a? Array }
|
35
|
-
actions = (options[:actions] - options[:exclude]) + options[:include]
|
36
|
-
links = []
|
37
|
-
|
38
|
-
object_array = Array(object_or_array)
|
39
|
-
object = object_array.last
|
40
|
-
object_name = object.class.model_name.human
|
41
|
-
namespaces = object_array.select{ |obj| obj.is_a? Symbol }
|
42
|
-
options[:controller_name] ||= [*namespaces, object.class.model_name.plural].compact.join('/')
|
43
|
-
controller = "#{options[:controller_name]}_controller".classify.constantize.new
|
44
|
-
|
45
|
-
links = actions.map do |action|
|
46
|
-
permission_passed = false
|
47
|
-
if respond_to? :permitted_to?
|
48
|
-
permission_passed = permitted_to?(action, object)
|
49
|
-
else
|
50
|
-
permission_passed = true
|
51
|
-
end
|
52
|
-
if controller.respond_to?(action) && permission_passed
|
53
|
-
url_array = implicit_route_actions.include?(action) ? object_array : [action, *object_array]
|
54
|
-
unless current_page?(url_array) && action != :destroy
|
55
|
-
title = I18n.t(action, :scope=>'action_links', :default=>action.to_s.humanize)
|
56
|
-
html_options = { :class=>action.to_s.parameterize, :title=>"#{title} #{object_name}" }
|
57
|
-
html_options[:remote] = true if options[:remote].include? action
|
58
|
-
|
59
|
-
if action == :destroy
|
60
|
-
html_options.merge! :method=>:delete, :confirm=>I18n.t(:confirm_delete, :scope=>'action_links', :default=>'Are you sure?')
|
61
|
-
end
|
62
|
-
|
63
|
-
link_to(title, url_array, html_options)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
links << capture(&block) if block_given?
|
69
|
-
|
70
|
-
return content_tag(options[:list_wrapper], :class=>:actions) do
|
71
|
-
links.compact.map { |link|
|
72
|
-
content_tag(options[:link_wrapper], link, :class=>options[:link_wrapper_class])
|
73
|
-
}.join("\n").html_safe
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
ActionView::Base.send :include, ActionLinks
|
78
5
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_links
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-11-22 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: Automatically includes action links (show, edit, destroy) based on the
|
16
16
|
current page and user roles/permissions
|
@@ -27,8 +27,11 @@ files:
|
|
27
27
|
- action_links.gemspec
|
28
28
|
- lib/action_links.rb
|
29
29
|
- lib/action_links/engine.rb
|
30
|
+
- lib/action_links/helpers.rb
|
31
|
+
- lib/action_links/link.rb
|
32
|
+
- lib/action_links/list.rb
|
30
33
|
- lib/action_links/version.rb
|
31
|
-
homepage: https://github.com/
|
34
|
+
homepage: https://github.com/biola/action_links
|
32
35
|
licenses: []
|
33
36
|
post_install_message:
|
34
37
|
rdoc_options: []
|