acts_as_indexable 0.0.1

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 (78) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +38 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +7 -0
  5. data/Gemfile +15 -0
  6. data/Gemfile.lock +197 -0
  7. data/Guardfile +42 -0
  8. data/LICENSE +22 -0
  9. data/README.md +95 -0
  10. data/Rakefile +23 -0
  11. data/acts_as_indexable.gemspec +31 -0
  12. data/app/helpers/acts_as_indexable/acts_as_indexable_helper.rb +12 -0
  13. data/app/views/acts_as_indexable/_index.html.erb +9 -0
  14. data/app/views/acts_as_indexable/_row.html.erb +15 -0
  15. data/app/views/acts_as_indexable/_thead.html.erb +7 -0
  16. data/lib/acts_as_indexable.rb +7 -0
  17. data/lib/acts_as_indexable/attribute.rb +59 -0
  18. data/lib/acts_as_indexable/engine.rb +4 -0
  19. data/lib/acts_as_indexable/version.rb +3 -0
  20. data/lib/acts_as_indexable/view.rb +26 -0
  21. data/spec/dummy/README.rdoc +28 -0
  22. data/spec/dummy/Rakefile +6 -0
  23. data/spec/dummy/app/assets/images/.keep +0 -0
  24. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  25. data/spec/dummy/app/assets/javascripts/widgets.js +2 -0
  26. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  27. data/spec/dummy/app/assets/stylesheets/widgets.css +4 -0
  28. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  29. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  30. data/spec/dummy/app/controllers/widgets_controller.rb +18 -0
  31. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  32. data/spec/dummy/app/helpers/widgets_helper.rb +2 -0
  33. data/spec/dummy/app/mailers/.keep +0 -0
  34. data/spec/dummy/app/models/.keep +0 -0
  35. data/spec/dummy/app/models/concerns/.keep +0 -0
  36. data/spec/dummy/app/models/widget.rb +3 -0
  37. data/spec/dummy/app/views/layouts/application.html.erb +17 -0
  38. data/spec/dummy/app/views/widgets/_date.html.erb +1 -0
  39. data/spec/dummy/app/views/widgets/index.html.erb +4 -0
  40. data/spec/dummy/bin/bundle +3 -0
  41. data/spec/dummy/bin/rails +4 -0
  42. data/spec/dummy/bin/rake +4 -0
  43. data/spec/dummy/bin/setup +29 -0
  44. data/spec/dummy/config.ru +4 -0
  45. data/spec/dummy/config/application.rb +32 -0
  46. data/spec/dummy/config/boot.rb +5 -0
  47. data/spec/dummy/config/database.yml +25 -0
  48. data/spec/dummy/config/environment.rb +5 -0
  49. data/spec/dummy/config/environments/development.rb +41 -0
  50. data/spec/dummy/config/environments/production.rb +79 -0
  51. data/spec/dummy/config/environments/test.rb +42 -0
  52. data/spec/dummy/config/initializers/assets.rb +11 -0
  53. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  54. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  55. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  56. data/spec/dummy/config/initializers/inflections.rb +16 -0
  57. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  58. data/spec/dummy/config/initializers/session_store.rb +3 -0
  59. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  60. data/spec/dummy/config/locales/en.yml +25 -0
  61. data/spec/dummy/config/routes.rb +4 -0
  62. data/spec/dummy/config/secrets.yml +22 -0
  63. data/spec/dummy/db/migrate/20151123200106_create_widgets.rb +10 -0
  64. data/spec/dummy/db/schema.rb +23 -0
  65. data/spec/dummy/db/seeds.rb +6 -0
  66. data/spec/dummy/lib/assets/.keep +0 -0
  67. data/spec/dummy/public/404.html +67 -0
  68. data/spec/dummy/public/422.html +67 -0
  69. data/spec/dummy/public/500.html +66 -0
  70. data/spec/dummy/public/favicon.ico +0 -0
  71. data/spec/factories/widget.rb +8 -0
  72. data/spec/features/acts_as_indexable/view_spec.rb +90 -0
  73. data/spec/helpers/acts_as_indexable_helper_spec.rb +16 -0
  74. data/spec/models/attribute_spec.rb +39 -0
  75. data/spec/spec_helper.rb +30 -0
  76. data/spec/support/database_cleaner.rb +13 -0
  77. data/spec/support/indexable_views.rb +3 -0
  78. metadata +402 -0
@@ -0,0 +1,31 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "acts_as_indexable/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "acts_as_indexable"
6
+ s.version = ActsAsIndexable::VERSION
7
+ s.authors = ["Taylor C. MacDonald"]
8
+ s.email = ["tcmacdonald@gmail.com"]
9
+ s.homepage = "https://github.com/tcmacdonald/acts_as_indexable"
10
+ s.summary = "Utilities for building and extending table views in your Rails application."
11
+ s.description = "ActsAsIndexable is a Rails engine that provides a configurable DSL for rendering tables in index views of your application. It works with any collection of enumerable objects, ActiveRecord or otherwise."
12
+ s.license = "MIT"
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.require_paths = ['lib','app']
17
+
18
+ s.add_runtime_dependency 'rails', '~> 4.2', '>= 4.2.3'
19
+
20
+ s.add_development_dependency 'sqlite3', '~> 1.3', '>= 1.3.11'
21
+ s.add_development_dependency 'rspec-rails', '~> 3.4', '>= 3.4.0'
22
+ s.add_development_dependency 'shoulda-matchers', '~> 3.0', '>= 3.0.1'
23
+ s.add_development_dependency 'database_cleaner', '~> 1.4', '>= 1.5.1'
24
+ s.add_development_dependency 'guard-rspec', '~> 4.6', '>= 4.6.4'
25
+ s.add_development_dependency 'launchy', '~> 2.4', '>= 2.4.3'
26
+ s.add_development_dependency 'capybara', '~> 2.5', '>= 2.5.0'
27
+ s.add_development_dependency 'capybara-webkit', '~> 1.7', '>= 1.7.1'
28
+ s.add_development_dependency 'factory_girl_rails', '~> 4.5', '>= 4.5.0'
29
+ s.add_development_dependency 'pry', '~> 0.10', '>= 0.10.3'
30
+
31
+ end
@@ -0,0 +1,12 @@
1
+ module ActsAsIndexable
2
+ module ActsAsIndexableHelper
3
+
4
+ def render_indexable(collection, **opts)
5
+ render partial: 'acts_as_indexable/index',
6
+ object: collection,
7
+ as: :collection,
8
+ locals: { opts: opts }
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ <%= content_tag :table, local_assigns.try(:[], :opts) do %>
2
+ <%= render 'acts_as_indexable/thead' %>
3
+ <% if local_assigns[:collection].present? %>
4
+ <tbody>
5
+ <%= render partial: 'acts_as_indexable/row', collection: local_assigns[:collection] %>
6
+ </tbody>
7
+ <% end %>
8
+ <% end %>
9
+
@@ -0,0 +1,15 @@
1
+ <tr id="<%= dom_id(row) %>">
2
+ <% decorated_attrs.each do |attr| %>
3
+ <%= content_tag :td, class: attr.try(:klass) do %>
4
+ <% if attr.partial.present? %>
5
+ <%= render partial: attr.partial, object: row %>
6
+ <% else %>
7
+ <% if attr.key == :actions %>
8
+ <%= attr.link_to_actions(row) %>
9
+ <% else %>
10
+ <%= link_to_if attr.path.present?, attr.l(row), attr.href(row) %>
11
+ <% end %>
12
+ <% end %>
13
+ <% end %>
14
+ <% end %>
15
+ </tr>
@@ -0,0 +1,7 @@
1
+ <thead>
2
+ <tr>
3
+ <% decorated_attrs.each do |attr| %>
4
+ <th><%= attr.label %></th>
5
+ <% end %>
6
+ </tr>
7
+ </thead>
@@ -0,0 +1,7 @@
1
+ require 'acts_as_indexable/attribute'
2
+ require 'acts_as_indexable/engine'
3
+ require 'acts_as_indexable/version'
4
+ require 'acts_as_indexable/view'
5
+
6
+ module ActsAsIndexable
7
+ end
@@ -0,0 +1,59 @@
1
+ module ActsAsIndexable
2
+ class Attribute
3
+ include ActionView::Helpers::UrlHelper
4
+
5
+ attr_accessor :key,
6
+ :label,
7
+ :attrs,
8
+ :format,
9
+ :partial,
10
+ :path
11
+
12
+ def initialize(key, attrs={})
13
+ @key = key
14
+ @attrs = attrs
15
+ @label = @attrs.try(:[], :label) || @key.to_s.humanize
16
+ @path = @attrs.try(:[], :link_to)
17
+ @format = @attrs.try(:[], :format)
18
+ @partial = @attrs.try(:[], :partial)
19
+ end
20
+
21
+ def l(ctx)
22
+ if @format.present?
23
+ I18n.l ctx.send(@key), format: @format
24
+ else
25
+ ctx.send(@key)
26
+ end
27
+ end
28
+
29
+ def href(ctx, href=nil)
30
+ path = href || @path
31
+ if path.present?
32
+ if path.to_s == 'self'
33
+ ctx
34
+ else
35
+ path.scan(/:[a-z_]*/).each do |attr|
36
+ path = path.gsub(/#{attr}/, ctx.send(attr[1..-1].to_sym).to_s)
37
+ end
38
+ path
39
+ end
40
+ end
41
+ end
42
+
43
+ def link_to_actions(ctx)
44
+ @attrs.to_h.collect do |k,v|
45
+ label = v.try(:[], :label) || k.to_s.humanize
46
+ opts = {}
47
+ if k == :delete
48
+ opts[:data] = { method: :delete, confirm: 'Are you sure?' }
49
+ path = v.try(:[], :link_to) || "/#{ctx.class.name.underscore.pluralize}/:id"
50
+ else
51
+ path = v.try(:[], :link_to) || "/#{ctx.class.name.underscore.pluralize}/:id/#{k}"
52
+ end
53
+
54
+ link_to label, href(ctx, path), opts.merge(v.except(:link_to, :label))
55
+ end.join().html_safe
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,4 @@
1
+ module ActsAsIndexable
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsIndexable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ module ActsAsIndexable
2
+ module View
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :decorated_attrs
7
+ end
8
+
9
+ protected
10
+
11
+ def current_attrs
12
+ @current_attrs ||= {
13
+ id: {},
14
+ to_s: {},
15
+ created_at: {},
16
+ }
17
+ end
18
+
19
+ def decorated_attrs
20
+ @decorated_attrs ||= current_attrs.collect do |k,v|
21
+ Attribute.new k, OpenStruct.new(v)
22
+ end
23
+ end
24
+
25
+ end
26
+ 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
File without changes
@@ -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,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -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,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -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,18 @@
1
+ class WidgetsController < ApplicationController
2
+ include ActsAsIndexable::View
3
+
4
+ protected
5
+
6
+ def current_attrs
7
+ {
8
+ id: {},
9
+ title: { link_to: :self },
10
+ body: {},
11
+ actions: {
12
+ edit: {},
13
+ delete: {}
14
+ }
15
+ }
16
+ end
17
+
18
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module WidgetsHelper
2
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,3 @@
1
+ class Widget < ActiveRecord::Base
2
+ alias_attribute :to_s, :title
3
+ end
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
6
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
7
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
8
+ <%= csrf_meta_tags %>
9
+ </head>
10
+ <body>
11
+
12
+ <div class="container">
13
+ <%= yield %>
14
+ </div>
15
+
16
+ </body>
17
+ </html>
@@ -0,0 +1 @@
1
+ <%= date.created_at.strftime('%B %Y') %>
@@ -0,0 +1,4 @@
1
+ <h1>Widgets#index</h1>
2
+
3
+ <%= render_indexable Widget.all, class: 'table table-striped' %>
4
+
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+
4
+ # path to your application root.
5
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
6
+
7
+ Dir.chdir APP_ROOT do
8
+ # This script is a starting point to setup your application.
9
+ # Add necessary setup steps to this file:
10
+
11
+ puts "== Installing dependencies =="
12
+ system "gem install bundler --conservative"
13
+ system "bundle check || bundle install"
14
+
15
+ # puts "\n== Copying sample files =="
16
+ # unless File.exist?("config/database.yml")
17
+ # system "cp config/database.yml.sample config/database.yml"
18
+ # end
19
+
20
+ puts "\n== Preparing database =="
21
+ system "bin/rake db:setup"
22
+
23
+ puts "\n== Removing old logs and tempfiles =="
24
+ system "rm -f log/*"
25
+ system "rm -rf tmp/cache"
26
+
27
+ puts "\n== Restarting application server =="
28
+ system "touch tmp/restart.txt"
29
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # Pick the frameworks you want:
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_mailer/railtie"
7
+ require "action_view/railtie"
8
+ require "sprockets/railtie"
9
+ # require "rails/test_unit/railtie"
10
+
11
+ Bundler.require(*Rails.groups)
12
+ require "acts_as_indexable"
13
+
14
+ module Dummy
15
+ class Application < Rails::Application
16
+ # Settings in config/environments/* take precedence over those specified here.
17
+ # Application configuration should go into files in config/initializers
18
+ # -- all .rb files in that directory are automatically loaded.
19
+
20
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
21
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
22
+ # config.time_zone = 'Central Time (US & Canada)'
23
+
24
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
25
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
26
+ # config.i18n.default_locale = :de
27
+
28
+ # Do not swallow errors in after_commit/after_rollback callbacks.
29
+ config.active_record.raise_in_transactional_callbacks = true
30
+ end
31
+ end
32
+
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,25 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ #
7
+ default: &default
8
+ adapter: sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ development:
13
+ <<: *default
14
+ database: db/development.sqlite3
15
+
16
+ # Warning: The database defined as "test" will be erased and
17
+ # re-generated from your development database when you run "rake".
18
+ # Do not set this db to the same as development or production.
19
+ test:
20
+ <<: *default
21
+ database: db/test.sqlite3
22
+
23
+ production:
24
+ <<: *default
25
+ database: db/production.sqlite3