pickel 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +21 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +93 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/gemfiles/activerecord_52.gemfile +5 -0
- data/gemfiles/activerecord_60.gemfile +5 -0
- data/gemfiles/activerecord_edge.gemfile +9 -0
- data/lib/pickel.rb +31 -0
- data/lib/pickel/condition.rb +55 -0
- data/lib/pickel/predicate.rb +86 -0
- data/lib/pickel/search.rb +56 -0
- data/lib/pickel/version.rb +5 -0
- data/lib/pickel/view_helper.rb +36 -0
- data/pickel.gemspec +36 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7fa31bd97a0458d27fea84bf3ca88f40d5dbb02ed1767425eb5b3604ede6f84a
|
4
|
+
data.tar.gz: fe14b35abfc0466b78714acd5fd824f6ac162f55a3296a230ca3e4dd35985efb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a408d3e7b92cc62e94bd8fe2b6c85ae62a1dbee88cbbb60f9685dc9fc3007213e3177c794ccae84aa17d5452207a5948e7401942a4ee80645fc27dcedefa76e1
|
7
|
+
data.tar.gz: 4252f85ab7ccc8d61b372ffd153a1ba9397f30e08a87be8b8362a6c5b77ae3ff2ce0d887afe53c5db7a6a15cec222055675c8698ef6468096f1083610ceb09fc
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
---
|
2
|
+
sudo: false
|
3
|
+
language: ruby
|
4
|
+
cache: bundler
|
5
|
+
rvm:
|
6
|
+
- 2.5
|
7
|
+
- 2.6
|
8
|
+
- ruby-head
|
9
|
+
before_install:
|
10
|
+
- gem update --system
|
11
|
+
- gem update bundler
|
12
|
+
|
13
|
+
gemfile:
|
14
|
+
- gemfiles/activerecord_52.gemfile
|
15
|
+
- gemfiles/activerecord_60.gemfile
|
16
|
+
- gemfiles/activerecord_edge.gemfile
|
17
|
+
|
18
|
+
matrix:
|
19
|
+
allow_failures:
|
20
|
+
- rvm: ruby-head
|
21
|
+
- gemfile: gemfiles/activerecord_edge.gemfile
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at sinsoku.listy@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 sinsoku
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/sinsoku/pickel.svg?branch=master)](https://travis-ci.org/sinsoku/pickel)
|
2
|
+
|
3
|
+
# Pickel
|
4
|
+
|
5
|
+
Pickel provides methods that make it easy to build a search form for your Rails app.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your Rails application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'pickel'
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
1. Allow parameters available for search.
|
18
|
+
2. Create an object with the `Pickel.search` method.
|
19
|
+
3. Create a search form with the `form_for` method.
|
20
|
+
|
21
|
+
### In controller
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
def index
|
25
|
+
search_params = Pickel.permit(params, :name, :age_gt, :posts_title_start)
|
26
|
+
@search = Pickel.search(User, search_params)
|
27
|
+
@users = @search.result
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
### In view
|
32
|
+
|
33
|
+
```erb
|
34
|
+
<%= form_for @search do |f| %>
|
35
|
+
<%# Search records contains the value %>
|
36
|
+
<%= f.label :name_cont %>
|
37
|
+
<%= f.search_field :name_cont %>
|
38
|
+
|
39
|
+
<%# Search records grater than the value %>
|
40
|
+
<%= f.label :age_gt %>
|
41
|
+
<%= f.number_field :age_gt %>
|
42
|
+
|
43
|
+
<%# Search records an associated posts.title starts with the value %>
|
44
|
+
<%= f.label :posts_title_start %>
|
45
|
+
<%= f.search_field :posts_title_start %>
|
46
|
+
|
47
|
+
<%= f.submit %>
|
48
|
+
<% end %>
|
49
|
+
```
|
50
|
+
|
51
|
+
## Support predicates
|
52
|
+
|
53
|
+
| Predicate | Description |
|
54
|
+
|---------------|------------------------------------|
|
55
|
+
| `*_eq` | equals |
|
56
|
+
| `*_not_eq` | not equal |
|
57
|
+
| `*_in` | in |
|
58
|
+
| `*_not_in` | not in |
|
59
|
+
| `*_lt` | less than |
|
60
|
+
| `*_lteq` | less than or equal |
|
61
|
+
| `*_gt` | greater than |
|
62
|
+
| `*_gteq` | greater than or equal |
|
63
|
+
| `*_matches` | matches with `LIKE` |
|
64
|
+
| `*_cont` | contains |
|
65
|
+
| `*_not_cont` | not contain |
|
66
|
+
| `*_start` | starts |
|
67
|
+
| `*_not_start` | not start |
|
68
|
+
| `*_end` | ends |
|
69
|
+
| `*_not_end` | not end |
|
70
|
+
| `*_true` | is `true` |
|
71
|
+
| `*_false` | is `false` |
|
72
|
+
| `*_null` | is `NULL` |
|
73
|
+
| `*_not_null` | is not `NULL` |
|
74
|
+
| `*_blank` | is `NULL` or empty string |
|
75
|
+
| `*_present` | is not `NULL` and not empty string |
|
76
|
+
|
77
|
+
## Development
|
78
|
+
|
79
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
80
|
+
|
81
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sinsoku/pickel. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
90
|
+
|
91
|
+
## Code of Conduct
|
92
|
+
|
93
|
+
Everyone interacting in the Pickel project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sinsoku/pickel/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "pickel"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/pickel.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
require 'active_support/lazy_load_hooks'
|
5
|
+
|
6
|
+
require "pickel/condition"
|
7
|
+
require "pickel/predicate"
|
8
|
+
require "pickel/search"
|
9
|
+
require "pickel/version"
|
10
|
+
|
11
|
+
module Pickel
|
12
|
+
class << self
|
13
|
+
def search(klass, search_params)
|
14
|
+
Search.new(klass.all, search_params)
|
15
|
+
end
|
16
|
+
|
17
|
+
def permit(params, *filters)
|
18
|
+
return {} unless params.key?(:q)
|
19
|
+
|
20
|
+
expanded = filters.flat_map do |f|
|
21
|
+
Predicate.find(f.to_s) ? f : Predicate.all.map { |predicate| "#{f}_#{predicate.id}".to_sym }
|
22
|
+
end
|
23
|
+
params[:q].permit(*expanded).to_h.reject { |_, v| v.blank? }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
ActiveSupport.on_load :action_controller do
|
29
|
+
require "pickel/view_helper"
|
30
|
+
::ActionController::Base.helper(Pickel::ViewHelper)
|
31
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pickel
|
4
|
+
class Condition
|
5
|
+
class << self
|
6
|
+
def for(klass, key)
|
7
|
+
s_key = key.to_s
|
8
|
+
predicate = Predicate.find(s_key)
|
9
|
+
|
10
|
+
cond = new(klass: klass, predicate: predicate)
|
11
|
+
new_key = s_key.delete_suffix("_#{predicate.id}")
|
12
|
+
cond.assign_column(new_key)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(klass:, predicate:, column: nil, target: nil, join_tables: [])
|
17
|
+
@klass = klass
|
18
|
+
@predicate = predicate
|
19
|
+
@column = column
|
20
|
+
@target = target
|
21
|
+
@join_tables = join_tables
|
22
|
+
end
|
23
|
+
attr_reader :klass, :predicate, :column, :target, :join_tables
|
24
|
+
|
25
|
+
def assign_column(key, target: klass, join_tables: [])
|
26
|
+
base = { klass: klass, predicate: predicate, target: target, join_tables: join_tables }
|
27
|
+
return Condition.new(base.merge(column: key)) if target.attribute_names.include?(key)
|
28
|
+
|
29
|
+
association = target.reflect_on_all_associations.find { |a| key.start_with?(a.name.to_s) }
|
30
|
+
if association
|
31
|
+
new_key = key.delete_prefix("#{association.name}_")
|
32
|
+
join_tables << association.name
|
33
|
+
new_target = association.klass
|
34
|
+
|
35
|
+
assign_column(new_key, target: new_target, join_tables: join_tables)
|
36
|
+
else
|
37
|
+
Condition.new(base.merge(predicate: nil))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def build(value)
|
42
|
+
return klass.all if predicate.nil?
|
43
|
+
|
44
|
+
rel = predicate.build(target, column, value)
|
45
|
+
join_tables.empty? ? rel : join_relation.merge(rel)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def join_relation
|
51
|
+
args = join_tables.reverse_each.inject { |a, b| { b => a } }
|
52
|
+
klass.joins(args)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pickel
|
4
|
+
class Predicate
|
5
|
+
AREL = %i[eq in not_eq not_in lt lteq gt gteq matches].freeze
|
6
|
+
MATCHES = %i[cont start end].flat_map { |x| [x, "not_#{x}".to_sym] }.freeze
|
7
|
+
LITERAL = %i[true false null].flat_map { |x| [x, "not_#{x}".to_sym] }.freeze
|
8
|
+
DERIVED = %i[blank present].freeze
|
9
|
+
ESCAPE_ADAPTERS = %w[Mysql2 PostgreSQL].freeze
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def all
|
13
|
+
@all ||= [AREL, MATCHES, LITERAL, DERIVED].flatten
|
14
|
+
.sort_by { |id| -id.size }.map { |id| new(id) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def find(id)
|
18
|
+
all.find { |predicate| id.end_with?("_#{predicate.id}") }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(id)
|
23
|
+
@id = id
|
24
|
+
end
|
25
|
+
attr_reader :id
|
26
|
+
|
27
|
+
def build(klass, column, value)
|
28
|
+
v = convert(value)
|
29
|
+
args =
|
30
|
+
case id
|
31
|
+
when *AREL
|
32
|
+
klass.arel_table[column].public_send(id, v)
|
33
|
+
when *MATCHES
|
34
|
+
klass.arel_table[column].matches(v)
|
35
|
+
when :blank
|
36
|
+
klass.arel_table[column].eq_any([nil, ''])
|
37
|
+
when :present
|
38
|
+
klass.arel_table[column].not_eq_all([nil, ''])
|
39
|
+
else
|
40
|
+
{ column => v }
|
41
|
+
end
|
42
|
+
|
43
|
+
when_not?(value) ? klass.where.not(args) : klass.where(args)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def convert(value)
|
49
|
+
case id
|
50
|
+
when :cont, :not_cont
|
51
|
+
"%#{escape_wildcards(value)}%"
|
52
|
+
when :start, :not_start
|
53
|
+
"#{escape_wildcards(value)}%"
|
54
|
+
when :end, :not_end
|
55
|
+
"%#{escape_wildcards(value)}"
|
56
|
+
when :true, :not_true
|
57
|
+
true
|
58
|
+
when :false, :not_false
|
59
|
+
false
|
60
|
+
when :null, :not_null
|
61
|
+
nil
|
62
|
+
else
|
63
|
+
value
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def cast_bool(value)
|
68
|
+
ActiveModel::Type::Boolean.new.cast(value)
|
69
|
+
end
|
70
|
+
|
71
|
+
# replace % \ to \% \\
|
72
|
+
def escape_wildcards(unescaped)
|
73
|
+
if ESCAPE_ADAPTERS.include?(ActiveRecord::Base.connection.adapter_name)
|
74
|
+
unescaped.to_s.gsub(/([\\%_.])/, '\\\\\\1')
|
75
|
+
else
|
76
|
+
unescaped
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def when_not?(value)
|
81
|
+
return false if AREL.include?(id)
|
82
|
+
|
83
|
+
id.to_s.start_with?('not') || !cast_bool(value)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pickel
|
4
|
+
class Search
|
5
|
+
include ActiveModel::Model
|
6
|
+
|
7
|
+
def initialize(relation, params = {})
|
8
|
+
@relation = relation
|
9
|
+
@klass = relation.klass
|
10
|
+
@params = params || {}
|
11
|
+
@sorts = []
|
12
|
+
@sorts << @params.delete(:s)
|
13
|
+
@sorts << @params.delete(:sorts)
|
14
|
+
@sorts.compact!
|
15
|
+
end
|
16
|
+
attr_reader :relation, :klass, :params, :sorts
|
17
|
+
alias s sorts
|
18
|
+
|
19
|
+
def sorts=(args)
|
20
|
+
@sorts = args
|
21
|
+
end
|
22
|
+
alias s= sorts=
|
23
|
+
|
24
|
+
def result
|
25
|
+
conditions = params.map { |k, v| Condition.for(klass, k).build(v) }
|
26
|
+
|
27
|
+
if sorts.empty?
|
28
|
+
conditions.inject(relation, :merge)
|
29
|
+
else
|
30
|
+
conditions.inject(relation, :merge).order(sorts)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def html_id
|
35
|
+
"#{klass.to_s.underscore}_search"
|
36
|
+
end
|
37
|
+
|
38
|
+
def respond_to?(name, include_all = false)
|
39
|
+
super || params.key?(name)
|
40
|
+
end
|
41
|
+
|
42
|
+
def method_missing(method_id, *args)
|
43
|
+
method_name = method_id.to_s
|
44
|
+
getter_name = method_name.delete_suffix('=')
|
45
|
+
predicate = Predicate.find(getter_name)
|
46
|
+
|
47
|
+
if predicate && method_name.end_with?('=')
|
48
|
+
params[getter_name] = *args
|
49
|
+
elsif predicate
|
50
|
+
params[getter_name]
|
51
|
+
else
|
52
|
+
super
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pickel
|
4
|
+
class FormBuilder < ActionView::Helpers::FormBuilder
|
5
|
+
def submit(value = nil, options = {})
|
6
|
+
value, options = nil, value if value.is_a?(Hash)
|
7
|
+
value ||= '検索' # TODO: I18n support
|
8
|
+
super(value, options)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ViewHelper
|
13
|
+
def form_with(model: nil, scope: nil, url: nil, format: nil, **options)
|
14
|
+
return super unless model.is_a?(Search)
|
15
|
+
|
16
|
+
url ||= polymorphic_path(model.klass, format: format)
|
17
|
+
scope ||= :q
|
18
|
+
options[:builder] ||= FormBuilder
|
19
|
+
options[:id] ||= model.html_id
|
20
|
+
options[:class] ||= model.html_id
|
21
|
+
options[:method] ||= :get
|
22
|
+
|
23
|
+
super(model: model, scope: scope, url: url, **options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def form_for(record, options = {}, &block)
|
27
|
+
return super unless record.is_a?(Search)
|
28
|
+
|
29
|
+
opts = options.dup
|
30
|
+
opts[:scope] = opts.delete(:as)
|
31
|
+
opts[:local] = !(opts.delete(:remote) == true)
|
32
|
+
|
33
|
+
form_with(model: record, **opts, &block)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/pickel.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "pickel/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "pickel"
|
9
|
+
spec.version = Pickel::VERSION
|
10
|
+
spec.authors = ["sinsoku"]
|
11
|
+
spec.email = ["sinsoku.listy@gmail.com"]
|
12
|
+
|
13
|
+
spec.summary = "A plugin that make it easy to build a search form"
|
14
|
+
spec.description = spec.summary
|
15
|
+
spec.homepage = "https://github.com/sinsoku/pickel"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "activerecord", ">= 5.0"
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
spec.add_development_dependency "sqlite3"
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pickel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- sinsoku
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-06-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A plugin that make it easy to build a search form
|
84
|
+
email:
|
85
|
+
- sinsoku.listy@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- gemfiles/activerecord_52.gemfile
|
101
|
+
- gemfiles/activerecord_60.gemfile
|
102
|
+
- gemfiles/activerecord_edge.gemfile
|
103
|
+
- lib/pickel.rb
|
104
|
+
- lib/pickel/condition.rb
|
105
|
+
- lib/pickel/predicate.rb
|
106
|
+
- lib/pickel/search.rb
|
107
|
+
- lib/pickel/version.rb
|
108
|
+
- lib/pickel/view_helper.rb
|
109
|
+
- pickel.gemspec
|
110
|
+
homepage: https://github.com/sinsoku/pickel
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata:
|
114
|
+
homepage_uri: https://github.com/sinsoku/pickel
|
115
|
+
source_code_uri: https://github.com/sinsoku/pickel
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubygems_version: 3.0.3
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: A plugin that make it easy to build a search form
|
135
|
+
test_files: []
|