search_object 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 185c2ec2237293e6a214c921457a18ef221402c395a74c66a774d2f8e2b4e12b
4
- data.tar.gz: 81dcfc6e5bfc8a3499cf24f37be6d2dd2fac23bdd6da39162a24ef66c20063bb
3
+ metadata.gz: ccb9a1eb55684f9df87c9d2c1ca76e20d946fb0c73d5fb52520c9c0b03bd41ab
4
+ data.tar.gz: b59ad0f1fd7e0bdbc2469a951ae7a6e36286690464897342cd954b3b4d6e9f54
5
5
  SHA512:
6
- metadata.gz: 2f66341b1d6e542396ac8930b3b29eba4124081e4e9e08d208208667856c80253ca6c33136afca304a47e2a2bbff03b4202170acd71e1da012058b0aaa05b144
7
- data.tar.gz: 1af8e023f69d4f30217a259018c6a866fb65ec26bf904ccbd28331cf62aa2bfce54cf937d8f9af24c30e2aa7a817eac0e7d51a9fcae6eb4f24fc737794eeea20
6
+ metadata.gz: ed7c3ca5011a75c1f43467b799af8914ff455f785060bb16733158ace7f24adb6f038aea6133c6de1c7fb04f6406c9bde8d1fddd88cc3b9628b9ba5f0347805c
7
+ data.tar.gz: 830399e3d6763d727911b5ea1279e7542c0fbb9608fad690d03e490e4329bc03fb201d05b2684f4e4aa0c7ca201e132feebc6e3f376fe413dda2d43b73c91bf3
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## Version 1.2.4
4
+
5
+ * __[feature]__ Added `:with` and block support to enum @hschne
6
+
3
7
  ## Version 1.2.3
4
8
 
5
9
  * __[fix]__ convert enum values to strings (@Postmodum37)
@@ -2,7 +2,7 @@
2
2
 
3
3
  source 'https://rubygems.org'
4
4
 
5
- gem 'rails', '5.0.1'
5
+ gem 'rails', '6.0.2.1'
6
6
 
7
7
  gem 'bootstrap-sass'
8
8
  gem 'jquery-rails'
@@ -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 CreateUsers < ActiveRecord::Migration
1
+ class CreateUsers < ActiveRecord::Migration[4.2]
2
2
  def change
3
3
  create_table :users do |t|
4
4
  t.string :name, null: false
@@ -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
 
@@ -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
- # 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).
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: 20131102130413) do
13
+ ActiveRecord::Schema.define(version: 2013_11_02_130413) do
15
14
 
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"
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
- 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
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
- raise BlockIgnoredError if block
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SearchObject
4
- VERSION = '1.2.3'
4
+ VERSION = '1.2.4'
5
5
  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 'raises when block is passed with enum option' do
62
- expect do
63
- Class.new do
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
- option(:filter, enum: %w[a b]) { |_scope, _value| nil }
67
- end
68
- end.to raise_error Enum::BlockIgnoredError
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 'raises when :with is passed with enum option' do
72
- expect do
73
- Class.new do
74
- include SearchObject.module(:enum)
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
- option :filter, enum: %w[a b], with: :method_name
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.to raise_error Enum::WithIgnoredError
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.3
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: 2019-10-27 00:00:00.000000000 Z
11
+ date: 2020-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler