link_to_action 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  require 'rails/generators'
2
2
  require 'rails/generators/named_base'
3
3
 
4
- module LinkToActions
4
+ module LinkToAction
5
5
  module Generators
6
6
  class InstallGenerator < Rails::Generators::Base
7
7
  desc "Copy LinkToAction configuration file"
@@ -0,0 +1,61 @@
1
+ module LinkToAction::Helpers
2
+ def link_to_new(object, options = {})
3
+ link_to_action(:new, object, options)
4
+ end
5
+
6
+ def link_to_edit(object, options = {})
7
+ link_to_action(:edit, object, options)
8
+ end
9
+
10
+ def link_to_destroy(object, options = {})
11
+ options[:method] = :delete
12
+ options[:data] = { :confirm => t(:'helpers.link_to.destroy_confirm') }
13
+ options['data-skip-pjax'] = true
14
+ link_to_action(:destroy, object, options)
15
+ end
16
+
17
+ def link_to_back(options = {})
18
+ ilink_to 'undo large', t(:'helpers.link_to.back'), :back, options
19
+ end
20
+
21
+ # TODO: Move to separate module to avoid clashes
22
+ private
23
+
24
+ # TODO: Move icon names to options
25
+ ICONS = {new: 'plus', edit: 'edit', destroy: 'trash'}
26
+
27
+ def link_to_action(action, object, options)
28
+ name = options.delete(:name) || t_action(object, action)
29
+ params = options.delete(:params) || {}
30
+ params[:action] = action if [ :new, :edit ].include? action
31
+ iilink_to "#{LinkToAction::Helpers::ICONS[action]} large", name,
32
+ polymorphic_path(object, params), options if cancan?(action, object)
33
+ end
34
+
35
+ def cancan?(*args)
36
+ LinkToAction.use_cancan ? can?(*args) : true
37
+ end
38
+
39
+ def iilink_to(icon_name, name, path, options = {})
40
+ if LinkToAction.use_icons
41
+ icon_class = icon_name.split(' ').map {|i| "icon-#{i}"}.join(' ')
42
+ icon = "<i class=\"#{icon_class}\"></i>"
43
+ name = raw("#{icon} #{ERB::Util.html_escape(name)}")
44
+ end
45
+ link_to name, path, options
46
+ end
47
+
48
+ # TODO: inspect some advanced I18n
49
+ # actionpack/lib/action_view/helpers/form_helper.rb, submit_default_value
50
+ def t_action(object, action)
51
+ model = if object.respond_to?(:model_name)
52
+ object.model_name.human
53
+ else
54
+ object.class.model_name.human
55
+ end
56
+
57
+ t(:"helpers.link_to.#{action}", model: model)
58
+ end
59
+ end
60
+
61
+ ActionView::Base.send :include, LinkToAction::Helpers
@@ -1,3 +1,3 @@
1
1
  module LinkToAction
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,61 +1,19 @@
1
1
  require "link_to_action/version"
2
+ require 'link_to_action/helpers'
2
3
  require 'action_view'
3
4
  require 'active_model'
4
5
  require 'active_support'
5
6
 
6
7
  module LinkToAction
7
- def link_to_new(object, options = {})
8
- link_to_action(:new, object, options)
9
- end
10
-
11
- def link_to_edit(object, options = {})
12
- link_to_action(:edit, object, options)
13
- end
14
-
15
- def link_to_destroy(object, options = {})
16
- options[:method] = :delete
17
- options[:data] = { :confirm => t(:'helpers.link_to.destroy_confirm') }
18
- options['data-skip-pjax'] = true
19
- link_to_action(:destroy, object, options)
20
- end
21
-
22
- def link_to_back(options = {})
23
- ilink_to 'undo large', t(:'helpers.link_to.back'), :back, options
24
- end
8
+ mattr_accessor :use_cancan
9
+ @@use_cancan = false
25
10
 
26
- private
11
+ mattr_accessor :use_icons
12
+ @@use_icons = false
27
13
 
28
- ICONS = {new: 'plus', edit: 'edit', destroy: 'trash'}
29
-
30
- def link_to_action(action, object, options)
31
- name = options.delete(:name) || t_action(object, action)
32
- params = options.delete(:params) || {}
33
- params[:action] = action if [ :new, :edit ].include? action
34
- # TODO: make icon and can? optional
35
- ilink_to "#{LinkToAction::ICONS[action]} large", name,
36
- polymorphic_path(object, params), options if can?(action, object)
37
- end
38
-
39
- def ilink_to(*args)
40
- icon = args[0].split(' ').map {|i| "icon-#{i}"}.join(' ')
41
- name = args[1]
42
- options = args.from(2)
43
- link_to raw("#{icon} #{ERB::Util.html_escape(name)}"), options
44
- end
45
-
46
- # TODO: inspect some advanced I18n
47
- # actionpack/lib/action_view/helpers/form_helper.rb, submit_default_value
48
- def t_action(object, action)
49
- model = if object.respond_to?(:model_name)
50
- object.model_name.human
51
- else
52
- object.class.model_name.human
53
- end
54
-
55
- t(:"helpers.link_to.#{action}", model: model)
14
+ def self.setup
15
+ yield self
56
16
  end
57
17
  end
58
18
 
59
- ActionView::Base.send :include, LinkToAction
60
-
61
19
  I18n.load_path << "#{File.dirname(__FILE__)}/link_to_action/locale/en.yml"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: link_to_action
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,6 +27,7 @@ files:
27
27
  - lib/generators/link_to_action/install_generator.rb
28
28
  - lib/generators/link_to_action/templates/link_to_action.rb
29
29
  - lib/link_to_action.rb
30
+ - lib/link_to_action/helpers.rb
30
31
  - lib/link_to_action/locale/en.yml
31
32
  - lib/link_to_action/version.rb
32
33
  - link_to_action.gemspec