search_object 1.2.3 → 1.2.4
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/example/Gemfile +1 -1
- data/example/app/models/post_search.rb +4 -4
- data/example/app/views/posts/index.html.slim +1 -1
- data/example/db/migrate/20131102130117_create_users.rb +1 -1
- data/example/db/migrate/20131102130413_create_posts.rb +1 -2
- data/example/db/schema.rb +19 -23
- data/lib/search_object/plugin/enum.rb +1 -4
- data/lib/search_object/version.rb +1 -1
- data/spec/search_object/plugin/enum_spec.rb +21 -13
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ccb9a1eb55684f9df87c9d2c1ca76e20d946fb0c73d5fb52520c9c0b03bd41ab
|
|
4
|
+
data.tar.gz: b59ad0f1fd7e0bdbc2469a951ae7a6e36286690464897342cd954b3b4d6e9f54
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ed7c3ca5011a75c1f43467b799af8914ff455f785060bb16733158ace7f24adb6f038aea6133c6de1c7fb04f6406c9bde8d1fddd88cc3b9628b9ba5f0347805c
|
|
7
|
+
data.tar.gz: 830399e3d6763d727911b5ea1279e7542c0fbb9608fad690d03e490e4329bc03fb201d05b2684f4e4aa0c7ca201e132feebc6e3f376fe413dda2d43b73c91bf3
|
data/CHANGELOG.md
CHANGED
data/example/Gemfile
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
class PostSearch
|
|
4
4
|
include SearchObject.module(:model, :sorting, :will_paginate, :enum)
|
|
5
5
|
|
|
6
|
-
scope { Post.all }
|
|
6
|
+
scope { Post.includes(:user).all }
|
|
7
7
|
|
|
8
8
|
sort_by :id, :created_at, :views_count, :likes_count, :comments_count
|
|
9
9
|
|
|
@@ -20,11 +20,11 @@ class PostSearch
|
|
|
20
20
|
option :rating, enum: %i[low high]
|
|
21
21
|
|
|
22
22
|
option :title do |scope, value|
|
|
23
|
-
scope.where 'title LIKE ?', escape_search_term(value)
|
|
23
|
+
scope.where 'title LIKE ?', escape_search_term(value) if value.present?
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
option :published do |scope, value|
|
|
27
|
-
scope.where published: true if value.present?
|
|
27
|
+
scope.where published: true if value.present? && value != '0'
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
option :created_after do |scope, value|
|
|
@@ -40,7 +40,7 @@ class PostSearch
|
|
|
40
40
|
private
|
|
41
41
|
|
|
42
42
|
def apply_term(scope, value)
|
|
43
|
-
scope.where 'title LIKE :term OR body LIKE :term', term: escape_search_term(value)
|
|
43
|
+
scope.where 'title LIKE :term OR body LIKE :term', term: escape_search_term(value) if value.present?
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
def apply_rating_with_low(scope)
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
th
|
|
33
33
|
th = form.text_field :title
|
|
34
34
|
th = form.select :user_id, User.all.map { |u| [u.name, u.id] }, include_blank: true
|
|
35
|
-
th = form.select :category_name, Post.pluck('DISTINCT category_name'), include_blank: true
|
|
35
|
+
th = form.select :category_name, Post.pluck(Arel.sql('DISTINCT category_name')), include_blank: true
|
|
36
36
|
th
|
|
37
37
|
th
|
|
38
38
|
th
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class CreatePosts < ActiveRecord::Migration
|
|
1
|
+
class CreatePosts < ActiveRecord::Migration[4.2]
|
|
2
2
|
def change
|
|
3
3
|
create_table :posts do |t|
|
|
4
4
|
t.integer :user_id, null: false
|
|
@@ -9,7 +9,6 @@ class CreatePosts < ActiveRecord::Migration
|
|
|
9
9
|
t.integer :likes_count, null: false, default: 0
|
|
10
10
|
t.integer :comments_count, null: false, default: 0
|
|
11
11
|
t.boolean :published, null: false, default: false
|
|
12
|
-
t.datetime :published_at
|
|
13
12
|
t.timestamps
|
|
14
13
|
end
|
|
15
14
|
|
data/example/db/schema.rb
CHANGED
|
@@ -1,40 +1,36 @@
|
|
|
1
|
-
# encoding: UTF-8
|
|
2
1
|
# This file is auto-generated from the current state of the database. Instead
|
|
3
2
|
# of editing this file, please use the migrations feature of Active Record to
|
|
4
3
|
# incrementally modify your database, and then regenerate this schema definition.
|
|
5
4
|
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
# from scratch.
|
|
10
|
-
#
|
|
5
|
+
# This file is the source Rails uses to define your schema when running `rails
|
|
6
|
+
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
|
|
7
|
+
# be faster and is potentially less error prone than running all of your
|
|
8
|
+
# migrations from scratch. Old migrations may fail to apply correctly if those
|
|
9
|
+
# migrations use external dependencies or application code.
|
|
11
10
|
#
|
|
12
11
|
# It's strongly recommended that you check this file into your version control system.
|
|
13
12
|
|
|
14
|
-
ActiveRecord::Schema.define(version:
|
|
13
|
+
ActiveRecord::Schema.define(version: 2013_11_02_130413) do
|
|
15
14
|
|
|
16
|
-
create_table "posts", force:
|
|
17
|
-
t.integer
|
|
18
|
-
t.string
|
|
19
|
-
t.string
|
|
20
|
-
t.string
|
|
21
|
-
t.integer
|
|
22
|
-
t.integer
|
|
23
|
-
t.integer
|
|
24
|
-
t.boolean
|
|
25
|
-
t.datetime "published_at"
|
|
15
|
+
create_table "posts", force: :cascade do |t|
|
|
16
|
+
t.integer "user_id", null: false
|
|
17
|
+
t.string "title", null: false
|
|
18
|
+
t.string "body", null: false
|
|
19
|
+
t.string "category_name", null: false
|
|
20
|
+
t.integer "views_count", default: 0, null: false
|
|
21
|
+
t.integer "likes_count", default: 0, null: false
|
|
22
|
+
t.integer "comments_count", default: 0, null: false
|
|
23
|
+
t.boolean "published", default: false, null: false
|
|
26
24
|
t.datetime "created_at"
|
|
27
25
|
t.datetime "updated_at"
|
|
26
|
+
t.index ["user_id"], name: "index_posts_on_user_id"
|
|
28
27
|
end
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
create_table "users", force: true do |t|
|
|
33
|
-
t.string "name", null: false
|
|
29
|
+
create_table "users", force: :cascade do |t|
|
|
30
|
+
t.string "name", limit: 255, null: false
|
|
34
31
|
t.datetime "created_at"
|
|
35
32
|
t.datetime "updated_at"
|
|
33
|
+
t.index ["name"], name: "index_users_on_name", unique: true
|
|
36
34
|
end
|
|
37
35
|
|
|
38
|
-
add_index "users", ["name"], name: "index_users_on_name", unique: true
|
|
39
|
-
|
|
40
36
|
end
|
|
@@ -11,10 +11,7 @@ module SearchObject
|
|
|
11
11
|
def option(name, options = nil, &block)
|
|
12
12
|
return super unless options.is_a?(Hash) && options[:enum]
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
raise WithIgnoredError if options[:with]
|
|
16
|
-
|
|
17
|
-
handler = Handler.build(name, options[:enum])
|
|
14
|
+
handler = options[:with] || block || Handler.build(name, options[:enum])
|
|
18
15
|
|
|
19
16
|
super(name, options, &handler)
|
|
20
17
|
end
|
|
@@ -58,24 +58,32 @@ module SearchObject
|
|
|
58
58
|
expect(test_search.results(filters: { camelCase: 'someValue' })).to eq [1]
|
|
59
59
|
end
|
|
60
60
|
|
|
61
|
-
it '
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
include SearchObject.module(:enum)
|
|
61
|
+
it 'can filter by passed block' do
|
|
62
|
+
block_search = Class.new do
|
|
63
|
+
include SearchObject.module(:enum)
|
|
65
64
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
scope { [1, 2, 3, 4, 5] }
|
|
66
|
+
|
|
67
|
+
option(:filter, enum: %w[odd even]) { |scope, value| scope.select(&:"#{value}?".to_sym) }
|
|
68
|
+
end
|
|
69
|
+
expect(block_search.results(filters: { filter: :odd })).to eq [1, 3, 5]
|
|
70
|
+
expect(block_search.results(filters: { filter: :even })).to eq [2, 4]
|
|
69
71
|
end
|
|
70
72
|
|
|
71
|
-
it '
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
it 'can filter by with option' do
|
|
74
|
+
with_search = Class.new do
|
|
75
|
+
include SearchObject.module(:enum)
|
|
76
|
+
|
|
77
|
+
scope { [1, 2, 3, 4, 5] }
|
|
75
78
|
|
|
76
|
-
|
|
79
|
+
option(:filter, enum: %w[odd even], with: :filter)
|
|
80
|
+
|
|
81
|
+
def filter(scope, value)
|
|
82
|
+
scope.select(&:"#{value}?".to_sym)
|
|
77
83
|
end
|
|
78
|
-
end
|
|
84
|
+
end
|
|
85
|
+
expect(with_search.results(filters: { filter: :odd })).to eq [1, 3, 5]
|
|
86
|
+
expect(with_search.results(filters: { filter: :even })).to eq [2, 4]
|
|
79
87
|
end
|
|
80
88
|
end
|
|
81
89
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: search_object
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.2.
|
|
4
|
+
version: 1.2.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Radoslav Stankov
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2020-03-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|