i18n_rails_helpers 0.8.2 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/contextual_link_helpers.rb +71 -0
- data/lib/i18n_rails_helpers/railtie.rb +2 -0
- metadata +6 -5
@@ -0,0 +1,71 @@
|
|
1
|
+
module ContextualLinkHelpers
|
2
|
+
# CRUD helpers
|
3
|
+
def icon_link_to(action, url, options = {})
|
4
|
+
options.merge!(:class => "icon icon-#{action}")
|
5
|
+
|
6
|
+
link_to(t_action(action), url, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
def contextual_link_to(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 icon_link_to(action, send("new_#{model_name}_path"), :remote => true)
|
31
|
+
when 'show'
|
32
|
+
return icon_link_to(action, send("#{model_name}_path", resource))
|
33
|
+
when 'edit'
|
34
|
+
return icon_link_to(action, send("edit_#{model_name}_path", resource))
|
35
|
+
when 'delete'
|
36
|
+
return icon_link_to(action, send("#{model_name}_path", resource), :confirm => t_confirm_delete(resource), :method => :delete)
|
37
|
+
when 'index'
|
38
|
+
return icon_link_to(action, send("#{model_name.pluralize}_path"))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def contextual_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
|
+
|
66
|
+
def contextual_links(action = nil, resource_or_model = nil)
|
67
|
+
content_tag('div', :class => 'contextual') do
|
68
|
+
contextual_links_for(action, resource_or_model)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -1,10 +1,12 @@
|
|
1
1
|
require 'i18n_rails_helpers'
|
2
|
+
require 'contextual_link_helpers'
|
2
3
|
require 'rails'
|
3
4
|
|
4
5
|
module I18nRailsHelpers
|
5
6
|
class Railtie < Rails::Engine
|
6
7
|
initializer :after_initialize do
|
7
8
|
ActionController::Base.helper I18nRailsHelpers
|
9
|
+
ActionController::Base.helper ContextualLinkHelpers
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 9
|
8
|
+
- 0
|
9
|
+
version: 0.9.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: 2010-
|
17
|
+
date: 2010-12-14 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- config/locales/de-CH.yml
|
57
57
|
- config/locales/de.yml
|
58
58
|
- config/locales/en.yml
|
59
|
+
- lib/contextual_link_helpers.rb
|
59
60
|
- lib/i18n_rails_helpers.rb
|
60
61
|
- lib/i18n_rails_helpers/railtie.rb
|
61
62
|
- rails/init.rb
|
@@ -92,5 +93,5 @@ signing_key:
|
|
92
93
|
specification_version: 3
|
93
94
|
summary: Rails i18n view helpers for things like crud actions, models and and attributes.
|
94
95
|
test_files:
|
95
|
-
- test/test_helper.rb
|
96
96
|
- test/i18n_rails_helpers_test.rb
|
97
|
+
- test/test_helper.rb
|