buttons_for 0.1.0

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.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/app/helpers/buttons_for/rails/buttons_for_helper.rb +128 -0
  5. data/lib/buttons_for.rb +3 -0
  6. data/lib/buttons_for/engine.rb +4 -0
  7. data/lib/buttons_for/version.rb +3 -0
  8. data/lib/generators/buttons_for/install_generator.rb +14 -0
  9. data/lib/generators/buttons_for/templates/buttons_for.en.yml +8 -0
  10. data/lib/tasks/buttons_for_tasks.rake +4 -0
  11. data/test/buttons_for_helper_test.rb +115 -0
  12. data/test/buttons_for_test.rb +7 -0
  13. data/test/dummy/README.rdoc +28 -0
  14. data/test/dummy/Rakefile +6 -0
  15. data/test/dummy/app/assets/javascripts/application.js +13 -0
  16. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  17. data/test/dummy/app/controllers/application_controller.rb +5 -0
  18. data/test/dummy/app/helpers/application_helper.rb +2 -0
  19. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  20. data/test/dummy/bin/bundle +3 -0
  21. data/test/dummy/bin/rails +4 -0
  22. data/test/dummy/bin/rake +4 -0
  23. data/test/dummy/bin/setup +29 -0
  24. data/test/dummy/config.ru +4 -0
  25. data/test/dummy/config/application.rb +26 -0
  26. data/test/dummy/config/boot.rb +5 -0
  27. data/test/dummy/config/database.yml +25 -0
  28. data/test/dummy/config/environment.rb +5 -0
  29. data/test/dummy/config/environments/development.rb +41 -0
  30. data/test/dummy/config/environments/production.rb +79 -0
  31. data/test/dummy/config/environments/test.rb +42 -0
  32. data/test/dummy/config/initializers/assets.rb +11 -0
  33. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  34. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  35. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  36. data/test/dummy/config/initializers/inflections.rb +16 -0
  37. data/test/dummy/config/initializers/mime_types.rb +4 -0
  38. data/test/dummy/config/initializers/session_store.rb +3 -0
  39. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  40. data/test/dummy/config/locales/buttons_for.en.yml +6 -0
  41. data/test/dummy/config/locales/en.yml +23 -0
  42. data/test/dummy/config/routes.rb +56 -0
  43. data/test/dummy/config/secrets.yml +22 -0
  44. data/test/dummy/db/development.sqlite3 +0 -0
  45. data/test/dummy/db/schema.rb +16 -0
  46. data/test/dummy/db/test.sqlite3 +0 -0
  47. data/test/dummy/log/development.log +4 -0
  48. data/test/dummy/log/test.log +13501 -0
  49. data/test/dummy/public/404.html +67 -0
  50. data/test/dummy/public/422.html +67 -0
  51. data/test/dummy/public/500.html +66 -0
  52. data/test/dummy/public/favicon.ico +0 -0
  53. data/test/test_helper.rb +20 -0
  54. metadata +187 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b6c11cb799b2692dd13249fc016f00069856bf9
4
+ data.tar.gz: 0144cd29a93f7eeff8704a6639838bba112eb881
5
+ SHA512:
6
+ metadata.gz: dc98e51c447eec371b31e6c5dc6bb08e72de9408eb5b32f4da2c333600cb7b24f0f685e5fe43d1c21ee5161e540a9f77efff4d1b76c1ac87c394447525702c7a
7
+ data.tar.gz: a9c26d13f023aab59bb1c84bc48cab80c0c7c051988bd325ea7b7113b57304f77423c79f73fe2ea04da50b42ea321ef3a13d87658615533347d81ee763549031
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Ole J. Rosendahl
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ButtonsFor'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,128 @@
1
+ module ButtonsFor
2
+ module Rails
3
+ module ButtonsForHelper
4
+
5
+ def buttons_for(options = {}, &block)
6
+ raise ArgumentError, "Missing block" unless block_given?
7
+
8
+ capture ButtonsForBuilder.new(self), &block
9
+ end
10
+
11
+ class ButtonsForBuilder
12
+ include ActionView::Helpers
13
+ include FontAwesome::Rails::IconHelper
14
+
15
+ attr_accessor :template, :output_buffer
16
+
17
+ def initialize(template)
18
+ @template = template
19
+ end
20
+
21
+ def button(text, url, options = {}, &block)
22
+ icon_options = extract_icon_options!(options)
23
+
24
+ link_to label(text, icon_options), url, options.merge(
25
+ class: classes(options),
26
+ title: t(text),
27
+ )
28
+ end
29
+
30
+ def new(url, options = {})
31
+ options[:label] ||= label(:new)
32
+
33
+ button options[:label], url, options.reverse_merge(
34
+ icon: 'plus',
35
+ class: 'btn-success'
36
+ )
37
+ end
38
+
39
+ def edit(url, options = {})
40
+ options[:label] ||= label(:edit)
41
+
42
+ button options[:label], url, options.reverse_merge(
43
+ class: 'btn-default',
44
+ icon: 'pencil'
45
+ )
46
+ end
47
+
48
+ def export(url, options = {})
49
+ options[:label] ||= label(:export)
50
+
51
+ button options[:label], url, options.reverse_merge(
52
+ class: 'btn-default',
53
+ icon: 'file-text-o'
54
+ )
55
+ end
56
+
57
+ def delete(url, options = {})
58
+ options[:label] ||= label(:delete)
59
+
60
+ button options[:label], url, options.reverse_merge(
61
+ class: 'btn-danger',
62
+ icon: 'trash-o',
63
+ method: :delete,
64
+ data: {
65
+ confirm: t(:confirm)
66
+ }
67
+ )
68
+ end
69
+
70
+ def dropdown(text, options = {}, &block)
71
+ raise ArgumentError, "Missing block" unless block_given?
72
+
73
+ icon_options = extract_icon_options!(options)
74
+
75
+ content_tag(:div, class: "btn-group") do
76
+ concat(content_tag(:button, label(text), class: "btn btn-default dropdown-toggle", data: {toggle: "dropdown"}, aria: {haspopup: "true", expanded: "false"}) do
77
+ "#{label(text, icon_options)} #{content_tag(:span, '', class: "caret")}".html_safe
78
+ end)
79
+ concat(content_tag(:ul, class: "dropdown-menu", "aria-labelledby" => text) do
80
+ concat template.capture(&block)
81
+ end)
82
+ end
83
+ end
84
+
85
+ def link(text, path, options = {})
86
+ options[:title] ||= label(text)
87
+
88
+ icon_options = extract_icon_options!(options)
89
+
90
+ content_tag(:li, link_to(label(text, icon_options), path, options))
91
+ end
92
+
93
+ private
94
+
95
+ def extract_icon_options!(options)
96
+ {}.tap do |icon_options|
97
+ icon_options[:icon] = options.delete(:icon) if options.key?(:icon)
98
+ end
99
+ end
100
+
101
+ def t(string)
102
+ I18n.t("buttons_for.#{string.to_s}", default: string.to_s.titleize)
103
+ end
104
+
105
+ def label(text, options = {})
106
+ label = text.is_a?(String) ? text : t(text)
107
+
108
+ if options[:icon]
109
+ fa_icon(options[:icon], text: label)
110
+ else
111
+ label
112
+ end
113
+ end
114
+
115
+ def classes(options)
116
+ "btn".tap do |s|
117
+ s << " #{options[:class]}" if options[:class]
118
+ end
119
+ end
120
+
121
+ def protect_against_forgery?
122
+ false
123
+ end
124
+
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,3 @@
1
+ require "buttons_for/version"
2
+ require "buttons_for/engine"
3
+ require "font-awesome-rails"
@@ -0,0 +1,4 @@
1
+ module ButtonsFor
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module ButtonsFor
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,14 @@
1
+ module ButtonsFor
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+
5
+ desc "Copy ButtonsFor translations"
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ def copy_translations
9
+ copy_file "buttons_for.en.yml", "config/locales/buttons_for.en.yml"
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ en:
2
+ buttons_for:
3
+ actions: "Actions"
4
+ new: "New"
5
+ edit: "Edit"
6
+ delete: "Delete"
7
+ export: "Export"
8
+ confirm: "Are you sure?"
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :buttons_for do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,115 @@
1
+ require "test_helper"
2
+
3
+ class ButtonsFor::Rails::ButtonsForHelperTest < ActionView::TestCase
4
+
5
+ test "#buttons_for(&block)" do
6
+ assert_raises(ArgumentError) { buttons_for }
7
+ end
8
+
9
+ test "#button" do
10
+ with_concat_buttons_for { |b| b.button "New", "#" }
11
+ assert_select "a.btn[href=\"#\"][title=\"New\"]", text: "New"
12
+ end
13
+
14
+ test "#button capitalize text if translation is symbol" do
15
+ with_concat_buttons_for { |b| b.button :capitalized_text, "#" }
16
+ assert_select "a.btn", text: "Capitalized Text"
17
+ end
18
+
19
+ test "#button uses the text directly if text is a string" do
20
+ with_concat_buttons_for { |b| b.button "capitalized text", "#" }
21
+ assert_select "a.btn", text: "capitalized text"
22
+ end
23
+
24
+ test "#button uses the translation if value exists for key" do
25
+ store_translations(:en, buttons_for: {close: "Lukk"}) do
26
+ with_concat_buttons_for { |b| b.button :close, "#" }
27
+ end
28
+ assert_select "a.btn", text: "Lukk"
29
+ end
30
+
31
+ test "#new" do
32
+ with_concat_buttons_for { |b| b.new "#" }
33
+ assert_select "a.btn.btn-success[href=\"#\"][title=\"New\"]", text: "New" do |element|
34
+ assert_select element, "i.fa.fa-plus"
35
+ end
36
+ end
37
+
38
+ test "#edit" do
39
+ with_concat_buttons_for { |b| b.edit "#" }
40
+ assert_select "a.btn.btn-default[href=\"#\"][title=\"Edit\"]", text: "Edit" do |element|
41
+ assert_select element, "i.fa.fa-pencil"
42
+ end
43
+ end
44
+
45
+ test "#delete" do
46
+ with_concat_buttons_for { |b| b.delete "#" }
47
+ assert_select "a.btn.btn[href=\"#\"][title=\"Delete\"][data-confirm=\"Are you sure?\"]", text: "Delete" do |element|
48
+ assert_select element, "i.fa.fa-trash-o"
49
+ end
50
+ end
51
+
52
+ test "#export" do
53
+ with_concat_buttons_for { |b| b.export "#" }
54
+ assert_select "a.btn.btn[href=\"#\"][title=\"Export\"]", text: "Export" do |element|
55
+ assert_select element, "i.fa.fa-file-text-o"
56
+ end
57
+ end
58
+
59
+ test "options[:label]" do
60
+ with_concat_buttons_for { |b| b.new "#", label: "New Project" }
61
+ assert_select "a", text: "New Project"
62
+ end
63
+
64
+ test "#dropdown with content generates at dropdown" do
65
+ with_concat_buttons_for do |b|
66
+ b.dropdown(:actions) { "content" }
67
+ end
68
+ assert_select "div.btn-group" do |btn_element|
69
+ assert_select btn_element, "button.btn.btn-default.dropdown-toggle[data-toggle=\"dropdown\"][aria-haspopup=\"true\"][aria-expanded=\"false\"]", text: "Actions"
70
+ assert_select btn_element, "span.caret"
71
+ assert_select btn_element, "ul.dropdown-menu[aria-labelledby=\"actions\"]", text: "content"
72
+ end
73
+ end
74
+
75
+ test "#dropdown options[:icon] prepends icon to label" do
76
+ with_concat_buttons_for do |b|
77
+ b.dropdown("New", icon: "plus") { "content" }
78
+ end
79
+ assert_select "i.fa.fa-plus"
80
+ end
81
+
82
+ test "#dropdown raises error of block is missing" do
83
+ assert_raises(ArgumentError) do
84
+ with_concat_buttons_for { |b| b.dropdown(:actions) }
85
+ end
86
+ end
87
+
88
+ test "#link" do
89
+ with_concat_buttons_for { |b| b.link "text", "#" }
90
+ assert_select "li a[href=\"#\"][title=\"text\"]", text: "text"
91
+ end
92
+
93
+ test "#link options[:icon]" do
94
+ with_concat_buttons_for do |b|
95
+ b.link "content", "#", icon: "shopping-cart"
96
+ end
97
+
98
+ assert_select "li a[href=\"#\"][title=\"content\"]"
99
+ end
100
+
101
+ private
102
+
103
+ def store_translations(locale, translations, &block)
104
+ I18n.backend.store_translations locale, translations
105
+ yield
106
+ ensure
107
+ I18n.reload!
108
+ I18n.backend.send :init_translations
109
+ end
110
+
111
+ def with_concat_buttons_for(*args, &block)
112
+ concat buttons_for(*args, &block)
113
+ end
114
+
115
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class ButtonsForTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, ButtonsFor
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>