link_to_action 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +13 -0
- data/lib/generators/link_to_action/install_generator.rb +5 -3
- data/lib/generators/link_to_action/templates/link_to_action.rb +4 -0
- data/lib/link_to_action/helpers.rb +26 -66
- data/lib/link_to_action/utils.rb +57 -0
- data/lib/link_to_action/version.rb +1 -1
- data/lib/link_to_action.rb +6 -0
- data/test/default_values_test.rb +3 -1
- data/test/dummy/log/test.log +4674 -0
- data/test/helpers_test.rb +34 -1
- metadata +5 -4
data/README.md
CHANGED
@@ -134,6 +134,8 @@ size_class_large = 'btn-large'
|
|
134
134
|
size_class_small = 'btn-small'
|
135
135
|
size_class_mini = 'btn-mini'
|
136
136
|
show_methods = [ :name, :title, :to_s ]
|
137
|
+
destroy_confirm = true
|
138
|
+
destroy_skip_pjax = false
|
137
139
|
```
|
138
140
|
Look to `config/initializers/link_to_action.rb` for detailed description.
|
139
141
|
|
@@ -149,6 +151,11 @@ Look to `config/initializers/link_to_action.rb` for detailed description.
|
|
149
151
|
:icon_swap # Swap default position of icon left-right
|
150
152
|
```
|
151
153
|
|
154
|
+
`link_to_destroy` options:
|
155
|
+
```ruby
|
156
|
+
:confirm # true/false or some text per-link basis
|
157
|
+
```
|
158
|
+
|
152
159
|
`link_to_show` options:
|
153
160
|
```ruby
|
154
161
|
:name # overwrites default name of the link
|
@@ -166,6 +173,12 @@ link_to_new Comment, params: {user_id: 1} # /comments/new?user_id=1
|
|
166
173
|
link_to_new [ @user, Comment ] # /users/1/comments/new
|
167
174
|
```
|
168
175
|
|
176
|
+
## TODO
|
177
|
+
|
178
|
+
1. Add templates to change Rails scaffold templates.
|
179
|
+
2. Better testing.
|
180
|
+
3. Improve README, add more examples.
|
181
|
+
|
169
182
|
## Contributing
|
170
183
|
|
171
184
|
1. Fork it
|
@@ -5,14 +5,16 @@ module LinkToAction
|
|
5
5
|
module Generators
|
6
6
|
class InstallGenerator < Rails::Generators::Base
|
7
7
|
desc "Copy LinkToAction configuration file"
|
8
|
-
source_root File.expand_path('
|
8
|
+
source_root File.expand_path('../../..', __FILE__)
|
9
9
|
|
10
10
|
def copy_initializers
|
11
|
-
copy_file 'link_to_action
|
11
|
+
copy_file 'generators/link_to_action/templates/link_to_action.rb',
|
12
|
+
'config/initializers/link_to_action.rb'
|
12
13
|
end
|
13
14
|
|
14
15
|
def copy_locale_file
|
15
|
-
copy_file '
|
16
|
+
copy_file 'link_to_action/locale/en.yml',
|
17
|
+
'config/locales/link_to_action.en.yml'
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# require 'ruby-debug'
|
2
2
|
# uncomment line above add 'debugger' somewhere (without quotes) to debug something
|
3
|
+
require_relative 'utils'
|
4
|
+
|
3
5
|
module LinkToAction::Helpers
|
4
6
|
def link_to_new(object, options = {})
|
5
7
|
link_to_action(:new, object, options)
|
@@ -15,8 +17,20 @@ module LinkToAction::Helpers
|
|
15
17
|
|
16
18
|
def link_to_destroy(object, options = {})
|
17
19
|
options[:method] = :delete
|
18
|
-
options
|
19
|
-
|
20
|
+
if (options.has_key? :confirm)
|
21
|
+
confirm = options.delete(:confirm)
|
22
|
+
else
|
23
|
+
confirm = LinkToAction.destroy_confirm
|
24
|
+
end
|
25
|
+
if confirm
|
26
|
+
if confirm.kind_of?(String)
|
27
|
+
confirm_text = confirm
|
28
|
+
else
|
29
|
+
confirm_text = t(:'helpers.link_to.destroy_confirm')
|
30
|
+
end
|
31
|
+
options[:data] = { :confirm => confirm_text }
|
32
|
+
end
|
33
|
+
options['data-skip-pjax'] = true if LinkToAction.destroy_skip_pjax
|
20
34
|
link_to_action(:destroy, object, options)
|
21
35
|
end
|
22
36
|
|
@@ -37,49 +51,22 @@ module LinkToAction::Helpers
|
|
37
51
|
link_to name, object, options
|
38
52
|
end
|
39
53
|
|
40
|
-
# TODO:
|
54
|
+
# TODO: Find the way to move this to separate module without loosing access to Rails helpers
|
55
|
+
# TODO: all these LinkToAction:: are not DRY, do something with this
|
41
56
|
private
|
42
57
|
|
43
|
-
def action_class(action, options)
|
44
|
-
if LinkToAction.use_classes
|
45
|
-
class_default = LinkToAction.class_default
|
46
|
-
class_action = LinkToAction.send("class_#{action}")
|
47
|
-
end
|
48
|
-
size = options.delete(:size) || 'default'
|
49
|
-
classes = []
|
50
|
-
classes = [ class_default, class_action ] unless class_action == ''
|
51
|
-
if options[:class]
|
52
|
-
classes = if LinkToAction.classes_append
|
53
|
-
classes.concat [ options[:class] ]
|
54
|
-
else
|
55
|
-
[ options[:class] ]
|
56
|
-
end
|
57
|
-
end
|
58
|
-
classes = classes.concat([ size_class(size) ]).compact.join(' ')
|
59
|
-
classes unless classes.blank?
|
60
|
-
end
|
61
|
-
|
62
|
-
def size_class(size)
|
63
|
-
LinkToAction.send("size_class_#{size}")
|
64
|
-
end
|
65
|
-
|
66
|
-
def action_icon(action, icon, icon_size)
|
67
|
-
icon_size = nil if icon_size == :default
|
68
|
-
[ icon, icon_size ].compact.map {|i| "icon-#{i}"}.join(' ') unless icon == ''
|
69
|
-
end
|
70
|
-
|
71
58
|
def link_to_action(action, object, options)
|
72
|
-
name = options.delete(:name) || t_action(object, action)
|
59
|
+
name = options.delete(:name) || LinkToAction::Utils::t_action(object, action)
|
73
60
|
params = options.delete(:params) || {}
|
74
61
|
params[:action] = action if [ :new, :edit ].include? action
|
75
|
-
options[:class] = action_class(action, options)
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
62
|
+
options[:class] = LinkToAction::Utils::action_class(action, options)
|
63
|
+
name = LinkToAction::Utils::add_icon_to_name(action, name, options)
|
64
|
+
if link_to_action_cancan?(action, object)
|
65
|
+
link_to name, link_to_action_path(action, object, params), options
|
66
|
+
end
|
80
67
|
end
|
81
68
|
|
82
|
-
def
|
69
|
+
def link_to_action_path(action, object, params)
|
83
70
|
if action == :back
|
84
71
|
action
|
85
72
|
else
|
@@ -87,36 +74,9 @@ module LinkToAction::Helpers
|
|
87
74
|
end
|
88
75
|
end
|
89
76
|
|
90
|
-
def
|
77
|
+
def link_to_action_cancan?(*args)
|
91
78
|
args[0] == :back || ( LinkToAction.use_cancan ? can?(*args) : true )
|
92
79
|
end
|
93
|
-
|
94
|
-
def iilink_to(icon, name, path, options = {})
|
95
|
-
icon_swap = options.delete(:icon_swap)
|
96
|
-
if LinkToAction.use_icons && icon
|
97
|
-
icon = "<i class=\"#{icon}\"></i>"
|
98
|
-
name = [icon, ERB::Util.html_escape(name) ]
|
99
|
-
name.reverse! unless LinkToAction.icons_place_left
|
100
|
-
name.reverse! if icon_swap
|
101
|
-
name = raw(name.join(' '))
|
102
|
-
end
|
103
|
-
link_to name, path, options
|
104
|
-
end
|
105
|
-
|
106
|
-
# TODO: inspect some advanced I18n
|
107
|
-
# actionpack/lib/action_view/helpers/form_helper.rb, submit_default_value
|
108
|
-
def t_action(object, action)
|
109
|
-
object = object.last if object.kind_of? Array
|
110
|
-
model = if object.respond_to?(:model_name)
|
111
|
-
object.model_name.human
|
112
|
-
else
|
113
|
-
object.class.model_name.human if object.class.respond_to?(:model_name)
|
114
|
-
end
|
115
|
-
|
116
|
-
model = model.pluralize if action == :index
|
117
|
-
|
118
|
-
t(:"helpers.link_to.#{action}", model: model)
|
119
|
-
end
|
120
80
|
end
|
121
81
|
|
122
82
|
ActionView::Base.send :include, LinkToAction::Helpers
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module LinkToAction::Utils
|
2
|
+
def self.action_icon(action, options)
|
3
|
+
icon = options.delete(:icon) || LinkToAction.send("icon_#{action}")
|
4
|
+
icon_size = options.delete(:icon_size) || LinkToAction.icons_size
|
5
|
+
icon_size = nil if icon_size == :default
|
6
|
+
[ icon, icon_size ].compact.map {|i| "icon-#{i}"}.join(' ') unless icon == ''
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.add_icon_to_name(action, name, options)
|
10
|
+
icon = self.action_icon(action, options)
|
11
|
+
icon_swap = options.delete(:icon_swap)
|
12
|
+
if LinkToAction.use_icons && icon
|
13
|
+
icon = "<i class=\"#{icon}\"></i>"
|
14
|
+
name = [icon, ERB::Util.html_escape(name) ]
|
15
|
+
name.reverse! unless LinkToAction.icons_place_left
|
16
|
+
name.reverse! if icon_swap
|
17
|
+
name.join(' ').html_safe
|
18
|
+
else
|
19
|
+
name
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.action_class(action, options)
|
24
|
+
if LinkToAction.use_classes
|
25
|
+
class_default = LinkToAction.class_default
|
26
|
+
class_action = LinkToAction.send("class_#{action}")
|
27
|
+
end
|
28
|
+
size = options.delete(:size) || 'default'
|
29
|
+
classes = []
|
30
|
+
classes = [ class_default, class_action ] unless class_action == ''
|
31
|
+
if options[:class]
|
32
|
+
classes = if LinkToAction.classes_append
|
33
|
+
classes.concat [ options[:class] ]
|
34
|
+
else
|
35
|
+
[ options[:class] ]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
size_class = [ LinkToAction.send("size_class_#{size}") ]
|
39
|
+
classes = classes.concat(size_class).compact.join(' ')
|
40
|
+
classes unless classes.blank?
|
41
|
+
end
|
42
|
+
|
43
|
+
# TODO: inspect some advanced I18n
|
44
|
+
# actionpack/lib/action_view/helpers/form_helper.rb, submit_default_value
|
45
|
+
def self.t_action(object, action)
|
46
|
+
object = object.last if object.kind_of? Array
|
47
|
+
model = if object.respond_to?(:model_name)
|
48
|
+
object.model_name.human
|
49
|
+
else
|
50
|
+
object.class.model_name.human if object.class.respond_to?(:model_name)
|
51
|
+
end
|
52
|
+
|
53
|
+
model = model.pluralize if action == :index
|
54
|
+
|
55
|
+
I18n.t(:"helpers.link_to.#{action}", model: model)
|
56
|
+
end
|
57
|
+
end
|
data/lib/link_to_action.rb
CHANGED
@@ -71,6 +71,12 @@ module LinkToAction
|
|
71
71
|
|
72
72
|
mattr_accessor :show_methods
|
73
73
|
@@show_methods = [ :name, :title, :to_s ]
|
74
|
+
|
75
|
+
mattr_accessor :destroy_confirm
|
76
|
+
@@destroy_confirm = true
|
77
|
+
|
78
|
+
mattr_accessor :destroy_skip_pjax
|
79
|
+
@@destroy_skip_pjax = false
|
74
80
|
|
75
81
|
def self.setup
|
76
82
|
yield self
|
data/test/default_values_test.rb
CHANGED
@@ -23,7 +23,9 @@ class DefaultValuesTest < ActiveSupport::TestCase
|
|
23
23
|
size_class_large: 'btn-large',
|
24
24
|
size_class_small: 'btn-small',
|
25
25
|
size_class_mini: 'btn-mini',
|
26
|
-
show_methods: [ :name, :title, :to_s ]
|
26
|
+
show_methods: [ :name, :title, :to_s ],
|
27
|
+
destroy_confirm: true,
|
28
|
+
destroy_skip_pjax: false
|
27
29
|
}
|
28
30
|
|
29
31
|
DEFAULT_VALUES.each do |key, value|
|