wobapphelpers 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +155 -0
  4. data/Rakefile +30 -0
  5. data/app/assets/stylesheets/wobapphelpers/breadcrumbs.scss +13 -0
  6. data/config/locales/de.yml +9 -0
  7. data/config/locales/en.yml +9 -0
  8. data/lib/generators/templates/erb/scaffold/_form.html.erb +26 -0
  9. data/lib/generators/templates/erb/scaffold/edit.html.erb +1 -0
  10. data/lib/generators/templates/erb/scaffold/index.html.erb +32 -0
  11. data/lib/generators/templates/erb/scaffold/new.html.erb +1 -0
  12. data/lib/generators/templates/erb/scaffold/show.html.erb +15 -0
  13. data/lib/generators/templates/initializers/wobapphelpers.rb +18 -0
  14. data/lib/generators/templates/layouts/application.html.erb +44 -0
  15. data/lib/generators/templates/rails/scaffold_controller/controller.rb +66 -0
  16. data/lib/generators/wobapphelpers/install_generator.rb +24 -0
  17. data/lib/generators/wobapphelpers/scaffold_templates_generator.rb +20 -0
  18. data/lib/tasks/wobapphelpers_tasks.rake +4 -0
  19. data/lib/wobapphelpers.rb +37 -0
  20. data/lib/wobapphelpers/breadcrumbs.rb +16 -0
  21. data/lib/wobapphelpers/breadcrumbs/action_controller.rb +57 -0
  22. data/lib/wobapphelpers/breadcrumbs/breadcrumbs_helper.rb +26 -0
  23. data/lib/wobapphelpers/helpers.rb +12 -0
  24. data/lib/wobapphelpers/helpers/action_view_helper.rb +170 -0
  25. data/lib/wobapphelpers/helpers/icon_helper.rb +39 -0
  26. data/lib/wobapphelpers/rails.rb +4 -0
  27. data/lib/wobapphelpers/responders.rb +13 -0
  28. data/lib/wobapphelpers/responders/locales/de.yml +12 -0
  29. data/lib/wobapphelpers/responders/locales/en.yml +12 -0
  30. data/lib/wobapphelpers/version.rb +4 -0
  31. data/test/breadcrumbs/action_controller_test.rb +44 -0
  32. data/test/breadcrumbs/breadcrumb_helper_test.rb +35 -0
  33. data/test/dummy/app/controllers/application_controller.rb +19 -0
  34. data/test/dummy/app/controllers/blogs_controller.rb +55 -0
  35. data/test/dummy/app/controllers/home_controller.rb +7 -0
  36. data/test/dummy/app/controllers/posts_controller.rb +58 -0
  37. data/test/dummy/app/helpers/application_helper.rb +3 -0
  38. data/test/dummy/app/helpers/blogs_helper.rb +2 -0
  39. data/test/dummy/app/helpers/posts_helper.rb +2 -0
  40. data/test/dummy/app/models/blog.rb +2 -0
  41. data/test/dummy/app/models/post.rb +2 -0
  42. data/test/dummy/config/application.rb +31 -0
  43. data/test/dummy/config/boot.rb +5 -0
  44. data/test/dummy/config/environment.rb +5 -0
  45. data/test/dummy/config/environments/development.rb +37 -0
  46. data/test/dummy/config/environments/production.rb +83 -0
  47. data/test/dummy/config/environments/test.rb +45 -0
  48. data/test/dummy/config/initializers/assets.rb +1 -0
  49. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  50. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  51. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  52. data/test/dummy/config/initializers/inflections.rb +16 -0
  53. data/test/dummy/config/initializers/mime_types.rb +4 -0
  54. data/test/dummy/config/initializers/session_store.rb +3 -0
  55. data/test/dummy/config/initializers/simple_form.rb +161 -0
  56. data/test/dummy/config/initializers/simple_form_bootstrap.rb +107 -0
  57. data/test/dummy/config/initializers/wobapphelpers.rb +9 -0
  58. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  59. data/test/dummy/config/routes.rb +61 -0
  60. data/test/dummy/db/migrate/20140420155942_create_posts.rb +12 -0
  61. data/test/dummy/db/migrate/20140516164637_create_blogs.rb +12 -0
  62. data/test/dummy/db/schema.rb +33 -0
  63. data/test/dummy/lib/templates/rails/scaffold_controller/controller.rb +65 -0
  64. data/test/generators/install_generator_test.rb +29 -0
  65. data/test/generators/scaffold_templates_generator_test.rb +16 -0
  66. data/test/helpers/action_view_helper_test.rb +41 -0
  67. data/test/helpers/active_class_helper_test.rb +9 -0
  68. data/test/helpers/can_view_helper_test.rb +172 -0
  69. data/test/helpers/flash_helper_test.rb +11 -0
  70. data/test/helpers/form_legend_test.rb +29 -0
  71. data/test/helpers/icon_helper_test.rb +21 -0
  72. data/test/helpers/show_edit_delete_link_helper_test.rb +45 -0
  73. data/test/integration/helper_delivery_test.rb +28 -0
  74. data/test/responders/flash_test.rb +23 -0
  75. data/test/test_helper.rb +42 -0
  76. data/test/tmp/lib/templates/rails/scaffold_controller/controller.rb +66 -0
  77. data/test/translate_test.rb +10 -0
  78. data/test/wobapphelpers_test.rb +20 -0
  79. metadata +371 -0
@@ -0,0 +1,24 @@
1
+ module Wobapphelpers
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ desc "Copy locale files to your application"
7
+
8
+ def copy_locale
9
+ ['en', 'de'].each do |lang|
10
+ copy_file "../../../config/locales/#{lang}.yml",
11
+ "config/locales/wobapphelpers.#{lang}.yml"
12
+ end
13
+ end
14
+
15
+ def copy_layout
16
+ copy_file "layouts/application.html.erb", "app/views/layouts/application.html.erb"
17
+ end
18
+
19
+ def copy_initializer
20
+ copy_file "initializers/wobapphelpers.rb", "config/initializers/wobapphelpers.rb"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ module Wobapphelpers
2
+ module Generators
3
+ class ScaffoldTemplatesGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ desc "Copy simple_form templates to your application"
7
+
8
+ def copy_view_templates
9
+ ["show.html.erb", "edit.html.erb", "new.html.erb", "index.html.erb", "_form.html.erb"].each do |file|
10
+ copy_file "erb/scaffold/#{file}", "lib/templates/erb/scaffold/#{file}"
11
+ end
12
+ end
13
+
14
+ def copy_controlle_template
15
+ copy_file "rails/scaffold_controller/controller.rb",
16
+ "lib/templates/rails/scaffold_controller/controller.rb"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :wobapphelpers do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,37 @@
1
+ module Wobapphelpers
2
+ autoload :Helpers, 'wobapphelpers/helpers'
3
+ autoload :Responders, 'wobapphelpers/responders'
4
+ autoload :Breadcrumbs, 'wobapphelpers/breadcrumbs'
5
+
6
+ def self.setup
7
+ yield self
8
+ end
9
+
10
+ # exclude controllers from breadcrumbs
11
+ #
12
+ mattr_accessor :breadcrumb_controller_blacklist
13
+ @@breadcrumb_controller_blacklist = []
14
+
15
+ # Use cancan abilities to decide if action links should be presented.
16
+ # Defaults to :none
17
+ #
18
+ mattr_reader :cancan
19
+ @@cancan = :none
20
+
21
+ # Which variant of cancan should be used? possible values are:
22
+ # * +:none+: no usage of Cancan or Cancancan
23
+ # * +:cancan1+: cancan '~> 1.6.0' or cancancan '~> 1.7.0'
24
+ # * +:cancan2+: cancan '~> 2.0.0'
25
+ #
26
+ def self.cancan=(variant)
27
+ if [:none, :cancan1, :cancan2].include?(variant)
28
+ @@cancan = variant
29
+ else
30
+ raise "Cancan variant #{variant} not implemented!"
31
+ end
32
+ end
33
+ end
34
+
35
+ if defined?(::Rails)
36
+ require 'wobapphelpers/rails'
37
+ end
@@ -0,0 +1,16 @@
1
+ require 'active_support/concern'
2
+
3
+ module Wobapphelpers
4
+ module Breadcrumbs
5
+ extend ActiveSupport::Concern
6
+
7
+ autoload :ActionController, 'wobapphelpers/breadcrumbs/action_controller.rb'
8
+ autoload :BreadcrumbsHelper, 'wobapphelpers/breadcrumbs/breadcrumbs_helper.rb'
9
+
10
+ included do
11
+ include Wobapphelpers::Breadcrumbs::ActionController
12
+ end
13
+ end
14
+ end
15
+
16
+ ActionView::Base.send :include, Wobapphelpers::Breadcrumbs::BreadcrumbsHelper
@@ -0,0 +1,57 @@
1
+ module Wobapphelpers
2
+ module Breadcrumbs
3
+ module ActionController
4
+ def add_breadcrumb(name, url)
5
+ breadcrumbs ||= (session[:breadcrumbs] || [] )
6
+ if params[:bci]
7
+ # this a back link of an existing breadcrumb entry,
8
+ # shorten the list
9
+ breadcrumbs = breadcrumbs.first(params[:bci].to_i)
10
+ end
11
+ if breadcrumbs.size == 0 || url != breadcrumbs.last[1]
12
+ breadcrumbs << [name, url]
13
+ end
14
+ session[:breadcrumbs] = breadcrumbs.last(6)
15
+ end
16
+
17
+ def last_breadcrumb
18
+ session[:breadcrumbs].last || [nil, :back]
19
+ end
20
+
21
+ def last_breadcrumb_url
22
+ last_breadcrumb[1]
23
+ end
24
+
25
+ def add_breadcrumb_index
26
+ return if request.format == :json
27
+ text = t("controller.#{controller_path}")
28
+ url = request.fullpath
29
+ add_breadcrumb(text, url)
30
+ end
31
+
32
+ def add_breadcrumb_show
33
+ resource = controller_name.singularize
34
+ if instance_variable_defined?("@#{resource}")
35
+ add_breadcrumb_for instance_variable_get("@#{resource}")
36
+ else
37
+ flash[:alert] = "add_breadcrumb_show: instance variable @#{resource} not set; see documentation for Wobapphelpers::Breadcrumbs"
38
+ end
39
+ end
40
+
41
+ def add_breadcrumb_for(poly)
42
+ if poly.is_a? Array
43
+ (parent, child) = poly
44
+ name = "#{t('activerecord.models.'+parent.class.name.underscore)}(#{parent.id})/"
45
+ if child.is_a? Symbol
46
+ name += "#{t('attributes.'+child.to_s)}"
47
+ else
48
+ name += "#{t('activerecord.models.'+child.class.name.underscore)}(#{child.id})"
49
+ end
50
+ else
51
+ name = "#{t('activerecord.models.' + poly.class.name.underscore)}(#{poly.id})"
52
+ end
53
+ add_breadcrumb name, polymorphic_path(poly)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,26 @@
1
+ module Wobapphelpers
2
+ module Breadcrumbs
3
+ module BreadcrumbsHelper
4
+ def render_breadcrumbs
5
+ return if Wobapphelpers.breadcrumb_controller_blacklist.include?(controller.controller_path)
6
+ return if session[:breadcrumbs].nil? || session[:breadcrumbs].size == 0
7
+ track = ""
8
+ session[:breadcrumbs].each_with_index do |b, idx|
9
+ track += "&gt; #{breadcrumb_idx(b[0], b[1], idx)} "
10
+ end
11
+ %Q[<div id="breadcrumbs" class="visible-desktop">#{track}</div>].html_safe
12
+ end
13
+
14
+ def breadcrumb_idx(name, url, idx, html_class=nil)
15
+ uri = URI.parse(url)
16
+ query = uri.query || ""
17
+ if query =~ /bci=(\d+)/
18
+ uri.query.gsub!(/bci=(\d+)/, "bci=#{idx}")
19
+ else
20
+ uri.query = query + "&bci=#{idx}"
21
+ end
22
+ link_to name, uri.to_s.gsub('?&','?'), :class => html_class
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ module Wobapphelpers
2
+ module Helpers
3
+ autoload :IconHelper, 'wobapphelpers/helpers/icon_helper.rb'
4
+ autoload :ActionViewHelper, 'wobapphelpers/helpers/action_view_helper.rb'
5
+
6
+ # one module to rule all
7
+ module All
8
+ include IconHelper
9
+ include ActionViewHelper
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,170 @@
1
+ module Wobapphelpers
2
+ module Helpers
3
+
4
+ # implement model independent links for new, edit, show, delete
5
+ module ActionViewHelper
6
+ include Wobapphelpers::Breadcrumbs::BreadcrumbsHelper
7
+
8
+ def form_legend
9
+ raw(
10
+ %Q[<div class="row">] +
11
+ %Q[<div class="col-sm-9 offset-sm-3 col-md-10 offset-md-2">] +
12
+ %Q[<legend>#{controlleraction}</legend>] +
13
+ %Q[</div>] +
14
+ %Q[</div>]
15
+ )
16
+ end
17
+
18
+ def new_link(poly, options = {})
19
+ mypoly, obj = get_parts(poly)
20
+ if _can?(:create, obj)
21
+ options.symbolize_keys!
22
+ link_to obj.model_name.human + " erstellen",
23
+ new_polymorphic_path(mypoly),
24
+ remote: options.fetch(:remote, false),
25
+ title: options.fetch(:title, title(obj) + " hinzufügen"),
26
+ class: options.fetch(:class, 'btn btn-secondary')
27
+ end
28
+ end
29
+
30
+ def show_link(poly, options = {})
31
+ mypoly, obj = get_parts(poly)
32
+ if _can?(:read, obj)
33
+ options.symbolize_keys!
34
+ link_to icon_show, polymorphic_path(mypoly),
35
+ remote: options.fetch(:remote, false),
36
+ title: options.fetch(:title, title(obj) + " anzeigen"),
37
+ class: options.fetch(:class, 'btn btn-secondary')
38
+ end
39
+ end
40
+
41
+ def edit_link(poly, options = {})
42
+ mypoly, obj = get_parts(poly)
43
+ if _can?(:edit, obj)
44
+ options.symbolize_keys!
45
+ link_to icon_edit, edit_polymorphic_path(mypoly),
46
+ remote: options.fetch(:remote, false),
47
+ title: options.fetch(:title, title(obj) + " bearbeiten"),
48
+ class: options.fetch(:class, 'btn btn-secondary')
49
+ end
50
+ end
51
+
52
+ def delete_link(poly, options = {})
53
+ mypoly, obj = get_parts(poly)
54
+ if _can?(:destroy, obj)
55
+ options.symbolize_keys!
56
+ verify = options.has_key?(:verify) ? { verify: options.fetch(:verify) } : {}
57
+ link_to icon_delete, mypoly,
58
+ remote: options.fetch(:remote, false),
59
+ data: {
60
+ confirm: options.fetch(:confirm, "Sie wollen das Objekt löschen.\nSind Sie sicher?")
61
+ }.merge(verify),
62
+ method: :delete,
63
+ title: options.fetch(:title, title(obj) + " löschen"),
64
+ class: options.fetch(:class, 'btn btn-danger')
65
+ end
66
+ end
67
+
68
+ def back_link(options = {})
69
+ options.symbolize_keys!
70
+ unless options[:path].nil?
71
+ goto = options[:path]
72
+ else
73
+ label = icon_left + " " + t('wobapphelpers.helpers.back')
74
+ if session[:breadcrumbs] && session[:breadcrumbs].size > 1
75
+ bc = session[:breadcrumbs][-2]
76
+ idx = session[:breadcrumbs].size - 2
77
+ title = bc[0]
78
+ goto = bc[1]
79
+ breadcrumb_idx(label, goto, idx, 'btn btn-secondary')
80
+ else
81
+ link_to label, url_for(:back), :class => 'btn btn-secondary'
82
+ end
83
+ end
84
+ end
85
+
86
+ def cancel_button
87
+ link_to icon_cancel + " " + t('wobapphelpers.helpers.cancel'),
88
+ url_for(:back), :class => 'btn btn-secondary'
89
+ end
90
+
91
+ def show_flash
92
+ # "- #{flash[:success]} - #{flash[:notice]} - #{flash[:alert]} -"
93
+ msg = ""
94
+ [:alert, :error, :notice, :success, :info].each do |severity|
95
+ next unless flash[severity]
96
+ case severity
97
+ when :error, :alert
98
+ my_class = "alert alert-danger"
99
+ when :notice, :info
100
+ my_class = "alert alert-info"
101
+ when :success
102
+ my_class = "alert alert-success"
103
+ else
104
+ my_class = severity.to_s
105
+ end
106
+ msg += %Q[<div id="#{severity.to_s}" class="#{my_class} alert-dismissable fade show noprint" role="alert">]
107
+ msg += %Q[<button type="button" class="close" data-dismiss="alert" aria-label="Clse">]
108
+ msg += %Q[<span aria-hidden="true">&times;</span>]
109
+ msg += %Q[</button>]
110
+ msg += flash[severity]
111
+ msg += %Q[</div>]
112
+ end
113
+ msg.html_safe
114
+ end
115
+
116
+ def active_class(myclass)
117
+ if controller.controller_name == myclass
118
+ "active"
119
+ end
120
+ end
121
+
122
+ private
123
+
124
+ #
125
+ # returns poly, object
126
+ # mypoly: poly.compact! if is_a? Array
127
+ #
128
+ def get_parts(poly)
129
+ if poly.kind_of? Array
130
+ mypoly = poly.compact
131
+ [mypoly, mypoly[-1]]
132
+ else
133
+ [poly, poly]
134
+ end
135
+ end
136
+
137
+ def title(obj)
138
+ if obj.kind_of? Class
139
+ model = obj
140
+ else
141
+ model = obj.class
142
+ end
143
+ t('activerecord.models.' + model.model_name.i18n_key.to_s)
144
+ end
145
+
146
+ def controlleraction
147
+ action = controller.action_name
148
+ namespace = controller.controller_path
149
+ resource_name = t("activerecord.models.#{namespace.singularize}")
150
+ search_for = [namespace, action].join(".").to_sym
151
+ t(search_for, scope: "wobapphelpers.controller".to_sym,
152
+ default: action.to_sym, name: resource_name)
153
+ end
154
+
155
+ def _can?(action, obj)
156
+ return true if Wobapphelpers.cancan == :none
157
+ obj = _normalize(obj) if obj.kind_of? Class
158
+ can? action, obj
159
+ end
160
+
161
+ def _normalize(model)
162
+ if Wobapphelpers.cancan == :cancan2
163
+ model.model_name.downcase.pluralize.to_sym
164
+ else
165
+ model
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,39 @@
1
+ module Wobapphelpers
2
+ module Helpers
3
+ module IconHelper
4
+ PREDEFINED_ICONS = {
5
+ attachment: 'file',
6
+ back: 'arrow-left',
7
+ calendar: 'calendar',
8
+ cancel: 'trash',
9
+ closed: 'lock',
10
+ copy: 'clone',
11
+ delete: 'trash',
12
+ document: 'file',
13
+ download: 'download',
14
+ edit: 'pencil',
15
+ help: 'question',
16
+ info: 'info',
17
+ left: 'arrow-left',
18
+ new: 'plus',
19
+ ok: 'check',
20
+ open: 'folder-open',
21
+ print: 'print',
22
+ right: 'arrow-right',
23
+ show: 'eye',
24
+ star: 'star',
25
+ up: 'arrow-up',
26
+ }
27
+
28
+ def icon(what)
29
+ raw(%Q[<i class="fa fa-#{what} fa-fw" aria-hidden="true"></i>])
30
+ end
31
+
32
+ PREDEFINED_ICONS.each do |key,value|
33
+ define_method :"icon_#{key}" do
34
+ icon(value)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,4 @@
1
+ module Wobapphelpers
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ require 'action_controller'
2
+ require 'responders'
3
+
4
+ module Wobapphelpers
5
+ class Responders < ActionController::Responder
6
+ include ::Responders::FlashResponder
7
+ end
8
+ end
9
+
10
+ # -- use own translations
11
+ require "active_support/i18n"
12
+ I18n.load_path += Dir[File.expand_path("../responders/locales/*.yml", __FILE__)]
13
+
@@ -0,0 +1,12 @@
1
+ de:
2
+ flash:
3
+ actions:
4
+ create:
5
+ notice: "%{resource_name} erfolgreich erstellt."
6
+ alert: "%{resource_name} konnte nicht angelegt werden!"
7
+ update:
8
+ notice: "%{resource_name} erfolgreich aktualisiert."
9
+ alert: "%{resource_name} konnte nicht aktualisiert werden!"
10
+ destroy:
11
+ notice: "%{resource_name} erfolgreich gelöscht."
12
+ alert: "%{resource_name} konnte nicht gelöscht werden!"