i18n_rails_helpers 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  require 'i18n_rails_helpers'
2
2
  require 'contextual_link_helpers'
3
+ require 'list_link_helpers'
3
4
  require 'rails'
4
5
 
5
6
  module I18nRailsHelpers
@@ -7,6 +8,7 @@ module I18nRailsHelpers
7
8
  initializer :after_initialize do
8
9
  ActionController::Base.helper I18nRailsHelpers
9
10
  ActionController::Base.helper ContextualLinkHelpers
11
+ ActionController::Base.helper ListLinkHelpers
10
12
  end
11
13
  end
12
14
  end
@@ -0,0 +1,65 @@
1
+ module ListLinkHelpers
2
+ # List link helpers
3
+ def list_link_to(action, url, options = {})
4
+ options.merge!(:class => "icon-#{action}-text", :title => t_action(action))
5
+
6
+ link_to(t_action(action), url, options)
7
+ end
8
+
9
+ def list_link_for(action, resource_or_model = nil)
10
+ # Handle both symbols and strings
11
+ action = action.to_s
12
+
13
+ # Resource and Model setup
14
+ # Use controller name to guess resource or model if not specified
15
+ case action
16
+ when 'new', 'index'
17
+ model = resource_or_model || controller_name.singularize.camelize.constantize
18
+ when 'show', 'edit', 'delete'
19
+ resource = resource_or_model || instance_variable_get("@#{controller_name.singularize}")
20
+ model = resource.class
21
+ end
22
+ model_name = model.to_s.underscore
23
+
24
+ # No link if CanCan is used and current user isn't authorized to call this action
25
+ return if respond_to?(:can?) and ! can?(action.to_sym, model)
26
+
27
+ # Link generation
28
+ case action
29
+ when 'new'
30
+ return list_link_to(action, send("new_#{model_name}_path"))
31
+ when 'show'
32
+ return list_link_to(action, send("#{model_name}_path", resource))
33
+ when 'edit'
34
+ return list_link_to(action, send("edit_#{model_name}_path", resource))
35
+ when 'delete'
36
+ return list_link_to(action, send("#{model_name}_path", resource), :confirm => t_confirm_delete(resource), :method => :delete)
37
+ when 'index'
38
+ return list_link_to(action, send("#{model_name.pluralize}_path"))
39
+ end
40
+ end
41
+
42
+ def list_links_for(action = nil, resource_or_model = nil)
43
+ # Use current action if not specified
44
+ action ||= action_name
45
+
46
+ # Handle both symbols and strings
47
+ action = action.to_s
48
+
49
+ actions = []
50
+ case action
51
+ when 'new', 'create'
52
+ actions << 'index'
53
+ when 'show'
54
+ actions += ['edit', 'delete', 'index']
55
+ when 'edit', 'update'
56
+ actions += ['show', 'delete', 'index']
57
+ when 'index'
58
+ actions << 'new'
59
+ end
60
+
61
+ links = actions.map{|link_for| contextual_link_to(link_for, resource_or_model)}
62
+
63
+ return links.join("\n").html_safe
64
+ end
65
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 10
7
+ - 11
8
8
  - 0
9
- version: 0.10.0
9
+ version: 0.11.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Simon H\xC3\xBCrlimann"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-04-11 00:00:00 +02:00
17
+ date: 2011-04-12 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,7 @@ files:
59
59
  - lib/contextual_link_helpers.rb
60
60
  - lib/i18n_rails_helpers.rb
61
61
  - lib/i18n_rails_helpers/railtie.rb
62
+ - lib/list_link_helpers.rb
62
63
  - rails/init.rb
63
64
  - test/i18n_rails_helpers_test.rb
64
65
  - test/test_helper.rb