greedo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +25 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +54 -0
  7. data/Rakefile +2 -0
  8. data/app/views/greedo/_grid.html.haml +17 -0
  9. data/greedo.gemspec +32 -0
  10. data/lib/greedo.rb +15 -0
  11. data/lib/greedo/engine.rb +4 -0
  12. data/lib/greedo/grid_helper.rb +96 -0
  13. data/lib/greedo/paginator.rb +42 -0
  14. data/lib/greedo/railtie.rb +7 -0
  15. data/lib/greedo/version.rb +3 -0
  16. data/spec/dummy/README.rdoc +28 -0
  17. data/spec/dummy/Rakefile +6 -0
  18. data/spec/dummy/app/assets/images/.keep +0 -0
  19. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  20. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  21. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  22. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  23. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  24. data/spec/dummy/app/mailers/.keep +0 -0
  25. data/spec/dummy/app/models/.keep +0 -0
  26. data/spec/dummy/app/models/concerns/.keep +0 -0
  27. data/spec/dummy/app/models/project.rb +2 -0
  28. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/spec/dummy/bin/bundle +3 -0
  30. data/spec/dummy/bin/rails +4 -0
  31. data/spec/dummy/bin/rake +4 -0
  32. data/spec/dummy/bin/setup +29 -0
  33. data/spec/dummy/config.ru +4 -0
  34. data/spec/dummy/config/application.rb +32 -0
  35. data/spec/dummy/config/boot.rb +5 -0
  36. data/spec/dummy/config/database.yml +25 -0
  37. data/spec/dummy/config/environment.rb +5 -0
  38. data/spec/dummy/config/environments/development.rb +41 -0
  39. data/spec/dummy/config/environments/production.rb +79 -0
  40. data/spec/dummy/config/environments/test.rb +42 -0
  41. data/spec/dummy/config/initializers/assets.rb +11 -0
  42. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  44. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  45. data/spec/dummy/config/initializers/inflections.rb +16 -0
  46. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  47. data/spec/dummy/config/initializers/session_store.rb +3 -0
  48. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/spec/dummy/config/locales/en.yml +23 -0
  50. data/spec/dummy/config/routes.rb +57 -0
  51. data/spec/dummy/config/secrets.yml +22 -0
  52. data/spec/dummy/db/development.sqlite3 +0 -0
  53. data/spec/dummy/db/migrate/20150918151106_create_projects.rb +9 -0
  54. data/spec/dummy/db/schema.rb +23 -0
  55. data/spec/dummy/db/test.sqlite3 +0 -0
  56. data/spec/dummy/lib/assets/.keep +0 -0
  57. data/spec/dummy/public/404.html +67 -0
  58. data/spec/dummy/public/422.html +67 -0
  59. data/spec/dummy/public/500.html +66 -0
  60. data/spec/dummy/public/favicon.ico +0 -0
  61. data/spec/greedo/grid_helper_spec.rb +124 -0
  62. data/spec/spec_helper.rb +14 -0
  63. metadata +294 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d3640b00c249ab0f3ec46c9effc6ea1a760c65c6
4
+ data.tar.gz: 7a527cd406993f475c47b08473f2cec9c18dcbae
5
+ SHA512:
6
+ metadata.gz: 642c57b5c69b8383670ad28aeea10255701c63f68d709b5b57b8f9ffe6f7dbfcd541b4b7e2c2ed842e8d1cc25a22477982a2d1ed6b6b719171814001617e72ea
7
+ data.tar.gz: bee6f1092985ca86be8e7bb09648324dd18bff12df13e2ad3bbc0bf413580e6f7aa0b2f0b302176e5cbb25d5a29a02f10cada07c864768a1355cbfc4b1f2d32f
@@ -0,0 +1,25 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .ruby-gemset
24
+ .ruby-version
25
+ spec/dummy/log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in greedo.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Adam Pohorecki
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ # Greedo
2
+
3
+ A very simple helper for generating data tables.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'greedo'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install greedo
18
+
19
+ ## Usage
20
+
21
+ After installing greedo, you can use the helper like this:
22
+
23
+ ```haml
24
+ %h1 My first table
25
+
26
+ = greedo(User.registered, per_page: 10) do |g|
27
+ = g.column :name
28
+ = g.column "Actions" do |user|
29
+ = link_to "Edit", edit_user_path(user)
30
+ ```
31
+
32
+ This will create a data table with two columns, one labelled "Name" and the other "Actions".
33
+ It will show 10 users from the given scope (which should either be an ActiveRecord::Relation or an Array).
34
+ Pagination will be added if necessary.
35
+
36
+ ## Limitations
37
+
38
+ This is a very simple helper for now - there's no sorting, or even any way to easily customize the generated HTML. This will change in time, but for now I'm open-sourcing this mainly to share this useful bit of code between projects.
39
+
40
+ ## TODO:
41
+
42
+ Here's a couple of things I'm planning to add to this gem:
43
+
44
+ * sorting by clicking on a column name
45
+ * a generator to install the templates used by greedo in your project for ease of customization
46
+ * making the paginator library swappable (greedo uses will_paginate now).
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/[my-github-username]/greedo/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,17 @@
1
+ - if grid.empty?
2
+ = grid.empty_message
3
+ - else
4
+ %table.table.table-striped
5
+ %thead
6
+ %tr
7
+ - grid.headers.each do |header|
8
+ %th= header.label
9
+
10
+ %tbody
11
+ - grid.rows.each do |row|
12
+ %tr{id: row.id}
13
+ - row.columns.each do |column|
14
+ %td= column.value
15
+
16
+ - if grid.show_pagination?
17
+ = will_paginate grid.paginator.records, param_name: param_name, renderer: BootstrapPagination::Rails
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'greedo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "greedo"
8
+ spec.version = Greedo::VERSION
9
+ spec.authors = ["Adam Pohorecki"]
10
+ spec.email = ["adam@pohorecki.pl"]
11
+ spec.summary = %q{A very simple grid library}
12
+ spec.description = %q{Shortens the syntax for creating tables}
13
+ spec.homepage = "https://github.com/gunpowderlabs/greedo"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec-core"
24
+ spec.add_development_dependency "rspec-rails"
25
+ spec.add_development_dependency "capybara"
26
+ spec.add_development_dependency "rails"
27
+ spec.add_development_dependency "sqlite3"
28
+
29
+ spec.add_dependency "haml"
30
+ spec.add_dependency "will_paginate"
31
+ spec.add_dependency "will_paginate-bootstrap"
32
+ end
@@ -0,0 +1,15 @@
1
+ require "haml"
2
+
3
+ require "greedo/version"
4
+ require "greedo/grid_helper"
5
+ require "greedo/paginator"
6
+ require "greedo/railtie"
7
+ require "greedo/engine"
8
+
9
+ module Greedo
10
+ def self.init
11
+ ActiveSupport.on_load(:action_view) do
12
+ ::ActionView::Base.send :include, Greedo::GridHelper
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ module Greedo
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,96 @@
1
+ module Greedo
2
+ module GridHelper
3
+ class Grid
4
+ attr_reader :paginator, :view_context, :fields
5
+
6
+ def initialize(paginator, view_context)
7
+ @paginator = paginator
8
+ @view_context = view_context
9
+ @row_id = ->(record) { default_row_id(record) }
10
+ @fields = []
11
+ end
12
+
13
+ def configure
14
+ yield self if block_given?
15
+ end
16
+
17
+ def row_id(&block)
18
+ @row_id = block
19
+ nil
20
+ end
21
+
22
+ def column(name, label: name.to_s.humanize, &block)
23
+ if block
24
+ renderer = ->(record) { view_context.capture(record, &block) }
25
+ else
26
+ renderer = ->(record) { record.public_send(name) }
27
+ end
28
+
29
+ fields << Field.new(name, label, renderer)
30
+ nil
31
+ end
32
+
33
+ def rows
34
+ records.map do |record|
35
+ Row.new(record, @row_id, fields)
36
+ end
37
+ end
38
+
39
+ def headers
40
+ fields
41
+ end
42
+
43
+ def empty?
44
+ records.empty?
45
+ end
46
+
47
+ def show_pagination?
48
+ paginator.show?
49
+ end
50
+
51
+ def empty_message
52
+ view_context.content_tag(:p) { "No data to show." }
53
+ end
54
+
55
+ private
56
+
57
+ def records
58
+ paginator.records
59
+ end
60
+
61
+ def default_row_id(record)
62
+ snake_class = record.class.name.underscore.gsub("_", "-")
63
+ "#{snake_class}-#{record.id}"
64
+ end
65
+
66
+ Column = Struct.new(:value)
67
+
68
+ Row = Struct.new(:record, :row_id, :fields) do
69
+ def id
70
+ row_id.call(record)
71
+ end
72
+
73
+ def columns
74
+ fields.map{ |f| Column.new(f.value(record)) }
75
+ end
76
+ end
77
+
78
+ Field = Struct.new(:name, :label, :renderer) do
79
+ def value(record)
80
+ renderer.call(record)
81
+ end
82
+ end
83
+ end
84
+
85
+ def greedo(scope,
86
+ param_name: :page,
87
+ page: params.fetch(param_name) { 1 }.to_i,
88
+ per_page: 20,
89
+ &block)
90
+ paginator = Paginator.build(scope, page: page, per_page: per_page)
91
+ grid = Grid.new(paginator, self)
92
+ grid.configure(&block)
93
+ render partial: "greedo/grid", locals: {grid: grid, param_name: param_name}
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,42 @@
1
+ require "will_paginate"
2
+ require "will_paginate-bootstrap"
3
+
4
+ module Greedo
5
+ class Paginator
6
+ attr_reader :scope, :page, :per_page
7
+
8
+ def self.build(scope,
9
+ params: {},
10
+ page: params.fetch(:page) { 1 }.to_i,
11
+ per_page: params.fetch(:per_page) { 20 }.to_i)
12
+ return ArrayPaginator.new(scope) if scope.is_a?(Array)
13
+ Paginator.new(scope, page: page, per_page: per_page)
14
+ end
15
+
16
+ def initialize(scope, page:, per_page:)
17
+ @scope = scope
18
+ @page = page
19
+ @per_page = per_page
20
+ end
21
+
22
+ def records
23
+ scope.paginate(page: page, per_page: per_page)
24
+ end
25
+
26
+ def show?
27
+ scope.count > per_page
28
+ end
29
+ end
30
+
31
+ class ArrayPaginator
32
+ attr_reader :records
33
+
34
+ def initialize(records)
35
+ @records = records
36
+ end
37
+
38
+ def show?
39
+ false
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,7 @@
1
+ module Greedo
2
+ class Railtie < ::Rails::Railtie
3
+ initializer 'greedo' do |_app|
4
+ Greedo.init
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Greedo
2
+ VERSION = "0.0.1"
3
+ 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,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
File without changes
File without changes
File without changes