action_links 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in action_links.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,45 @@
1
+ == Action_Links
2
+
3
+ Action_Links automatically includes action links (show, edit, destroy) based on the current page and user roles/permissions.
4
+
5
+
6
+ == Installation
7
+
8
+ Action_Links has been tested through Rails 3.0.10.
9
+
10
+ Add the following to your Gemfile
11
+
12
+ gem "action_links"
13
+
14
+ From the command line
15
+
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
+
23
+
24
+ == Usage
25
+
26
+ * Basic Usage
27
+
28
+ =action_links @user
29
+
30
+ * Include actions
31
+
32
+ =action_links @user, :include => [:download]
33
+
34
+ * Exclude actions
35
+
36
+ =action_links @user, :exclude => [:edit, :destroy]
37
+
38
+ * Pass in block
39
+
40
+ =action_links @user do
41
+ =link_to "Hello World", dashboard_path
42
+
43
+ * Namespaced objects
44
+
45
+ =action_links [:manage, @product]
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "action_links/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "action_links"
7
+ s.version = ActionLinks::VERSION
8
+ s.authors = ["Adam Crownoble", "Ryan Hall"]
9
+ s.email = ["adam@obledesign.com"]
10
+ s.homepage = "https://github.com/halloffame/action_links"
11
+ s.summary = %q{Quick and painless action links}
12
+ s.description = %q{Automatically includes action links (show, edit, destroy) based on the current page and user roles/permissions}
13
+
14
+ s.rubyforge_project = "action_links"
15
+
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
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,78 @@
1
+ require "action_links/version"
2
+
3
+ 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
+ end
@@ -0,0 +1,7 @@
1
+ require 'action_links'
2
+ require 'rails'
3
+
4
+ module ActionLinks
5
+ class Engine < Rails::Engine
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module ActionLinks
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_links
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Crownoble
9
+ - Ryan Hall
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2011-09-27 00:00:00.000000000Z
14
+ dependencies: []
15
+ description: Automatically includes action links (show, edit, destroy) based on the
16
+ current page and user roles/permissions
17
+ email:
18
+ - adam@obledesign.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - .gitignore
24
+ - Gemfile
25
+ - README.rdoc
26
+ - Rakefile
27
+ - action_links.gemspec
28
+ - lib/action_links.rb
29
+ - lib/action_links/engine.rb
30
+ - lib/action_links/version.rb
31
+ homepage: https://github.com/halloffame/action_links
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project: action_links
51
+ rubygems_version: 1.8.10
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Quick and painless action links
55
+ test_files: []