search_object 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 (70) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +8 -0
  4. data/CHANGELOG.md +5 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +283 -0
  8. data/Rakefile +6 -0
  9. data/example/.gitignore +16 -0
  10. data/example/.rspec +1 -0
  11. data/example/Gemfile +11 -0
  12. data/example/README.md +34 -0
  13. data/example/Rakefile +6 -0
  14. data/example/app/assets/javascripts/application.js +5 -0
  15. data/example/app/assets/stylesheets/application.css.scss +40 -0
  16. data/example/app/assets/stylesheets/reset.css +43 -0
  17. data/example/app/controllers/application_controller.rb +3 -0
  18. data/example/app/controllers/posts_controller.rb +5 -0
  19. data/example/app/models/.keep +0 -0
  20. data/example/app/models/post.rb +13 -0
  21. data/example/app/models/post_search.rb +44 -0
  22. data/example/app/models/user.rb +5 -0
  23. data/example/app/views/layouts/application.html.slim +12 -0
  24. data/example/app/views/posts/index.html.slim +48 -0
  25. data/example/bin/bundle +3 -0
  26. data/example/bin/rails +4 -0
  27. data/example/bin/rake +4 -0
  28. data/example/config.ru +4 -0
  29. data/example/config/application.rb +27 -0
  30. data/example/config/boot.rb +4 -0
  31. data/example/config/database.yml +12 -0
  32. data/example/config/environment.rb +5 -0
  33. data/example/config/environments/development.rb +29 -0
  34. data/example/config/environments/test.rb +37 -0
  35. data/example/config/initializers/filter_parameter_logging.rb +4 -0
  36. data/example/config/initializers/secret_token.rb +12 -0
  37. data/example/config/initializers/session_store.rb +3 -0
  38. data/example/config/initializers/wrap_parameters.rb +14 -0
  39. data/example/config/routes.rb +3 -0
  40. data/example/db/migrate/20131102130117_create_users.rb +10 -0
  41. data/example/db/migrate/20131102130413_create_posts.rb +18 -0
  42. data/example/db/schema.rb +40 -0
  43. data/example/db/seeds.rb +37 -0
  44. data/example/log/.keep +0 -0
  45. data/example/screenshot.png +0 -0
  46. data/example/spec/models/post_search_spec.rb +81 -0
  47. data/example/spec/spec_helper.rb +19 -0
  48. data/lib/search_object.rb +20 -0
  49. data/lib/search_object/base.rb +64 -0
  50. data/lib/search_object/helper.rb +36 -0
  51. data/lib/search_object/plugin/kaminari.rb +18 -0
  52. data/lib/search_object/plugin/model.rb +16 -0
  53. data/lib/search_object/plugin/paging.rb +42 -0
  54. data/lib/search_object/plugin/sorting.rb +54 -0
  55. data/lib/search_object/plugin/will_paginate.rb +17 -0
  56. data/lib/search_object/search.rb +26 -0
  57. data/lib/search_object/version.rb +3 -0
  58. data/search_object.gemspec +31 -0
  59. data/spec/search_object/base_spec.rb +237 -0
  60. data/spec/search_object/helper_spec.rb +30 -0
  61. data/spec/search_object/plugin/kaminari_spec.rb +50 -0
  62. data/spec/search_object/plugin/model_spec.rb +22 -0
  63. data/spec/search_object/plugin/paging_spec.rb +43 -0
  64. data/spec/search_object/plugin/sorting_spec.rb +139 -0
  65. data/spec/search_object/plugin/will_paginate_spec.rb +51 -0
  66. data/spec/search_object/search_spec.rb +72 -0
  67. data/spec/spec_helper.rb +13 -0
  68. data/spec/spec_helper_active_record.rb +19 -0
  69. data/spec/support/kaminari_setup.rb +7 -0
  70. metadata +292 -0
@@ -0,0 +1,43 @@
1
+ html, body, div, span, applet, object, iframe,
2
+ h1, h2, h3, h4, h5, h6, p, blockquote, pre,
3
+ a, abbr, acronym, address, big, cite, code,
4
+ del, dfn, em, img, ins, kbd, q, s, samp,
5
+ small, strike, strong, sub, sup, tt, var,
6
+ b, u, i, center,
7
+ dl, dt, dd, ol, ul, li,
8
+ fieldset, form, label, legend,
9
+ table, caption, tbody, tfoot, thead, tr, th, td,
10
+ article, aside, canvas, details, embed,
11
+ figure, figcaption, footer, header, hgroup,
12
+ menu, nav, output, ruby, section, summary,
13
+ time, mark, audio, video {
14
+ margin: 0;
15
+ padding: 0;
16
+ border: 0;
17
+ font-size: 100%;
18
+ font: inherit;
19
+ vertical-align: baseline;
20
+ }
21
+ /* HTML5 display-role reset for older browsers */
22
+ article, aside, details, figcaption, figure,
23
+ footer, header, hgroup, menu, nav, section {
24
+ display: block;
25
+ }
26
+ body {
27
+ line-height: 1;
28
+ }
29
+ ol, ul {
30
+ list-style: none;
31
+ }
32
+ blockquote, q {
33
+ quotes: none;
34
+ }
35
+ blockquote:before, blockquote:after,
36
+ q:before, q:after {
37
+ content: '';
38
+ content: none;
39
+ }
40
+ table {
41
+ border-collapse: collapse;
42
+ border-spacing: 0;
43
+ }
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery with: :exception
3
+ end
@@ -0,0 +1,5 @@
1
+ class PostsController < ApplicationController
2
+ def index
3
+ @search = PostSearch.new params[:f], params[:page]
4
+ end
5
+ end
File without changes
@@ -0,0 +1,13 @@
1
+ class Post < ActiveRecord::Base
2
+ belongs_to :user
3
+
4
+ validates :user, presence: true
5
+ validates :title, presence: true
6
+ validates :body, presence: true
7
+ validates :category_name, presence: true
8
+ validates :views_count, numericality: true
9
+ validates :likes_count, numericality: true
10
+ validates :comments_count, numericality: true
11
+
12
+ delegate :name, to: :user, prefix: true
13
+ end
@@ -0,0 +1,44 @@
1
+ class PostSearch
2
+ include SearchObject.module(:model, :sorting, :will_paginate)
3
+
4
+ scope { Post.all }
5
+
6
+ sort_by :created_at, :views_count, :likes_count, :comments_count
7
+
8
+ per_page 15
9
+
10
+ option :user_id
11
+ option :category_name
12
+
13
+ option :title do |scope, value|
14
+ scope.where 'title LIKE ?', escape_search_term(value)
15
+ end
16
+
17
+ option :published do |scope, value|
18
+ scope.where published: true if value.present?
19
+ end
20
+
21
+ option :term do |scope, value|
22
+ scope.where 'title LIKE :term OR body LIKE :term', term: escape_search_term(value)
23
+ end
24
+
25
+ option :created_after do |scope, value|
26
+ date = parse_date value
27
+ scope.where('DATE(created_at) >= ?', date) if date.present?
28
+ end
29
+
30
+ option :created_before do |scope, value|
31
+ date = parse_date value
32
+ scope.where('DATE(created_at) <= ?', date) if date.present?
33
+ end
34
+
35
+ private
36
+
37
+ def parse_date(date)
38
+ Date.strptime(date, '%Y-%m-%d') rescue nil
39
+ end
40
+
41
+ def escape_search_term(term)
42
+ "%#{term.gsub(/\s+/, '%')}%"
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :posts
3
+
4
+ validates :name, presence: true, uniqueness: true
5
+ end
@@ -0,0 +1,12 @@
1
+ doctype html
2
+ html
3
+ head
4
+ title SearchObject example
5
+
6
+ = stylesheet_link_tag 'application', media: 'all'
7
+ = csrf_meta_tags
8
+
9
+ body
10
+ = yield
11
+
12
+ = javascript_include_tag 'application'
@@ -0,0 +1,48 @@
1
+ = form_for @search, as: :f, method: :get, url: root_path, html: {id: 'search-form'} do |form|
2
+ = form.hidden_field :sort
3
+
4
+ h1 Listing #{pluralize @search.count, 'post'}
5
+
6
+ fieldset
7
+ = form.search_field :term, placeholder: 'Search term'
8
+ = date_field_tag 'f[created_after]', @search.created_after
9
+ = date_field_tag 'f[created_before]', @search.created_before
10
+ = form.submit 'Search'
11
+
12
+ table.table.table-striped
13
+ thead
14
+ tr
15
+ th = form.label :title, 'Title'
16
+ th = form.label :user_id, 'Author'
17
+ th = form.label :category_name, 'Category'
18
+ th = link_to 'Views', root_path(f: @search.sort_params_for(:views_count)), class: @search.sort?(:views_count) && 'active'
19
+ th = link_to 'Likes', root_path(f: @search.sort_params_for(:likes_count)), class: @search.sort?(:likes_count) && 'active'
20
+ th = link_to 'Comments', root_path(f: @search.sort_params_for(:comments_count)), class: @search.sort?(:comments_count) && 'active'
21
+ th = form.label :published, 'Published?'
22
+ th = link_to 'Created at', root_path(f: @search.sort_params_for(:created_at)), class: @search.sort?(:created_at) && 'active'
23
+ tr
24
+ th = form.text_field :title
25
+ th = form.select :user_id, User.all.map { |u| [u.name, u.id] }, include_blank: true
26
+ th = form.select :category_name, Post.pluck('DISTINCT category_name'), include_blank: true
27
+ th
28
+ th
29
+ th
30
+ th = form.check_box :published
31
+ th
32
+ tbody
33
+ - if @search.results?
34
+ - @search.results.each do |post|
35
+ tr
36
+ td = post.title
37
+ td = post.user_name
38
+ td = post.category_name
39
+ td = post.views_count
40
+ td = post.likes_count
41
+ td = post.comments_count
42
+ td = post.published ? 'Yes' : 'No'
43
+ td = l post.created_at, format: :short
44
+ - else
45
+ tr
46
+ td.empty colspan="8" No posts found
47
+
48
+ .pull-right = will_paginate @search.results, renderer: BootstrapPagination::Rails
@@ -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,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,27 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ # Load SearchObject manually from lib
6
+ $:.unshift File.expand_path('../../../lib', __FILE__)
7
+ require 'search_object'
8
+
9
+ # Require the gems listed in Gemfile, including any gems
10
+ # you've limited to :test, :development, or :production.
11
+ Bundler.require(:default, Rails.env)
12
+
13
+ module Example
14
+ class Application < Rails::Application
15
+ # Settings in config/environments/* take precedence over those specified here.
16
+ # Application configuration should go into files in config/initializers
17
+ # -- all .rb files in that directory are automatically loaded.
18
+
19
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
20
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
21
+ # config.time_zone = 'Central Time (US & Canada)'
22
+
23
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
24
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
25
+ # config.i18n.default_locale = :de
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
@@ -0,0 +1,12 @@
1
+ development:
2
+ adapter: sqlite3
3
+ database: db/development.sqlite3
4
+ pool: 5
5
+ timeout: 5000
6
+
7
+ test:
8
+ adapter: sqlite3
9
+ database: db/test.sqlite3
10
+ pool: 5
11
+ timeout: 5000
12
+
@@ -0,0 +1,5 @@
1
+ # Load the Rails application.
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the Rails application.
5
+ Example::Application.initialize!
@@ -0,0 +1,29 @@
1
+ Example::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Do not eager load code on boot.
10
+ config.eager_load = false
11
+
12
+ # Show full error reports and disable caching.
13
+ config.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Don't care if the mailer can't send.
17
+ config.action_mailer.raise_delivery_errors = false
18
+
19
+ # Print deprecation notices to the Rails logger.
20
+ config.active_support.deprecation = :log
21
+
22
+ # Raise an error on page load if there are pending migrations
23
+ config.active_record.migration_error = :page_load
24
+
25
+ # Debug mode disables concatenation and preprocessing of assets.
26
+ # This option may cause significant delays in view rendering with a large
27
+ # number of complex assets.
28
+ config.assets.debug = true
29
+ end
@@ -0,0 +1,37 @@
1
+ Example::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Do not eager load code on boot. This avoids loading your whole application
11
+ # just for the purpose of running a single test. If you are using a tool that
12
+ # preloads Rails for running tests, you may have to set it to true.
13
+ config.eager_load = false
14
+
15
+ # Configure static asset server for tests with Cache-Control for performance.
16
+ config.serve_static_assets = true
17
+ config.static_cache_control = "public, max-age=3600"
18
+
19
+ # Show full error reports and disable caching.
20
+ config.consider_all_requests_local = true
21
+ config.action_controller.perform_caching = false
22
+
23
+ # Raise exceptions instead of rendering exception templates.
24
+ config.action_dispatch.show_exceptions = false
25
+
26
+ # Disable request forgery protection in test environment.
27
+ config.action_controller.allow_forgery_protection = false
28
+
29
+ # Tell Action Mailer not to deliver emails to the real world.
30
+ # The :test delivery method accumulates sent emails in the
31
+ # ActionMailer::Base.deliveries array.
32
+ config.action_mailer.delivery_method = :test
33
+
34
+ # Print deprecation notices to the stderr.
35
+ config.active_support.deprecation = :stderr
36
+ end
37
+
@@ -0,0 +1,4 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Configure sensitive parameters which will be filtered from the log file.
4
+ Rails.application.config.filter_parameters += [:password]
@@ -0,0 +1,12 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key is used for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+
6
+ # Make sure the secret is at least 30 characters and all random,
7
+ # no regular words or you'll be exposed to dictionary attacks.
8
+ # You can use `rake secret` to generate a secure secret key.
9
+
10
+ # Make sure your secret_key_base is kept private
11
+ # if you're sharing your code publicly.
12
+ Example::Application.config.secret_key_base = 'de7dcb2c40f416eb022b006d3156fed75e5398e601c5c9219bde8181cb63d51a330ddca8580fd534038d5d7c69b7d0afa396f5f80538e94275d3e4917c7654e6'
@@ -0,0 +1,3 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Example::Application.config.session_store :cookie_store, key: '_example_session'
@@ -0,0 +1,14 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
9
+ end
10
+
11
+ # To enable root element in JSON for ActiveRecord objects.
12
+ # ActiveSupport.on_load(:active_record) do
13
+ # self.include_root_in_json = true
14
+ # end
@@ -0,0 +1,3 @@
1
+ Example::Application.routes.draw do
2
+ root to: 'posts#index'
3
+ end
@@ -0,0 +1,10 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :name, null: false
5
+ t.timestamps
6
+ end
7
+
8
+ add_index :users, :name, unique: true
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ class CreatePosts < ActiveRecord::Migration
2
+ def change
3
+ create_table :posts do |t|
4
+ t.integer :user_id, null: false
5
+ t.string :title, null: false
6
+ t.string :body, null: false
7
+ t.string :category_name, null: false
8
+ t.integer :views_count, null: false, default: 0
9
+ t.integer :likes_count, null: false, default: 0
10
+ t.integer :comments_count, null: false, default: 0
11
+ t.boolean :published, null: false, default: false
12
+ t.datetime :published_at
13
+ t.timestamps
14
+ end
15
+
16
+ add_index :posts, :user_id
17
+ end
18
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended that you check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(version: 20131102130413) do
15
+
16
+ create_table "posts", force: true do |t|
17
+ t.integer "user_id", null: false
18
+ t.string "title", null: false
19
+ t.string "body", null: false
20
+ t.string "category_name", null: false
21
+ t.integer "views_count", default: 0, null: false
22
+ t.integer "likes_count", default: 0, null: false
23
+ t.integer "comments_count", default: 0, null: false
24
+ t.boolean "published", default: false, null: false
25
+ t.datetime "published_at"
26
+ t.datetime "created_at"
27
+ t.datetime "updated_at"
28
+ end
29
+
30
+ add_index "posts", ["user_id"], name: "index_posts_on_user_id"
31
+
32
+ create_table "users", force: true do |t|
33
+ t.string "name", null: false
34
+ t.datetime "created_at"
35
+ t.datetime "updated_at"
36
+ end
37
+
38
+ add_index "users", ["name"], name: "index_users_on_name", unique: true
39
+
40
+ end