rad_core_rails 0.5.4 → 0.7.0
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/.gitignore +2 -1
- data/.rspec +3 -3
- data/.rubocop.yml +4 -0
- data/.travis.yml +5 -5
- data/CODE_OF_CONDUCT.md +74 -74
- data/LICENSE.txt +21 -21
- data/README.md +61 -1
- data/Rakefile +5 -0
- data/lib/generators/rad_core_rails/templates/migration.rb.tt +17 -0
- data/lib/generators/rad_core_rails/zip_codes_migration_generator.rb +18 -0
- data/lib/rad_core_rails.rb +10 -7
- data/lib/rad_core_rails/query_generator.rb +41 -5
- data/lib/rad_core_rails/railtie.rb +7 -0
- data/lib/rad_core_rails/search_terms.rb +30 -30
- data/lib/rad_core_rails/sortable.rb +14 -14
- data/lib/rad_core_rails/version.rb +1 -1
- data/lib/rad_core_rails/zip_code.rb +54 -0
- data/lib/rad_core_rails/zip_importer.rb +43 -0
- data/lib/resources/us_zip_codes.json +102 -0
- data/lib/tasks/rad_core_rails.rake +14 -0
- data/rad_core_rails.gemspec +8 -3
- metadata +72 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c80461bc2b08411eb1989bbda0e49d9cb47e73fbcd3b959c569742dd75f3342
|
4
|
+
data.tar.gz: 7df243406dd934723cf34c117475b3384a0bd237486d390236ea0e9215671234
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e2f659480bbf9fe6b5eedbeb4a8c007c7877ee572709334a63441bbaeefd6f28488712010532eb7edb086028ad62714568b222a95f293ad387416917b05b098
|
7
|
+
data.tar.gz: 9e3071cd56281bb7f2dcbd22a1c5ff0d106803233f6e537ea5c5f600ae0432bf18edfd99ea136a80aea6290114b6eefb76afbd7e7ba031c03ed104b041575ad0
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
--format documentation
|
2
|
-
--color
|
3
|
-
--require spec_helper
|
1
|
+
--format documentation
|
2
|
+
--color
|
3
|
+
--require spec_helper
|
data/.rubocop.yml
ADDED
data/.travis.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
---
|
2
|
-
language: ruby
|
3
|
-
cache: bundler
|
4
|
-
rvm:
|
5
|
-
- 2.4.5
|
1
|
+
---
|
2
|
+
language: ruby
|
3
|
+
cache: bundler
|
4
|
+
rvm:
|
5
|
+
- 2.4.5
|
6
6
|
- 2.7.0
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -1,74 +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 sancho.ck@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 [https://contributor-covenant.org/version/1/4][version]
|
72
|
-
|
73
|
-
[homepage]: https://contributor-covenant.org
|
74
|
-
[version]: https://contributor-covenant.org/version/1/4/
|
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 sancho.ck@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 [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/LICENSE.txt
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
The MIT License (MIT)
|
2
|
-
|
3
|
-
Copyright (c) 2020 Oleksandr Poltavets
|
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.
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Oleksandr Poltavets
|
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
CHANGED
@@ -22,7 +22,67 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
For basic functionality just include `RadCoreRails` to your `ActiveRecord` model.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class Job < ActiveRecord::Base
|
29
|
+
include RadCoreRails
|
30
|
+
...
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
For using Zip Codes by Radius filter follow the steps below:
|
35
|
+
|
36
|
+
Step 1 - generate migration:
|
37
|
+
|
38
|
+
```shell script
|
39
|
+
rails generate rad_core_rails:zip_codes_migration
|
40
|
+
```
|
41
|
+
|
42
|
+
Step 2 - migrate DB:
|
43
|
+
|
44
|
+
```shell script
|
45
|
+
rails db:migrate
|
46
|
+
```
|
47
|
+
|
48
|
+
Step 3 - import all US zip-codes to your DB:
|
49
|
+
> this gem has built in JSON file with all US zip-codes
|
50
|
+
|
51
|
+
```shell script
|
52
|
+
rails rad_core_rails:import_zip_codes
|
53
|
+
```
|
54
|
+
|
55
|
+
Step 4 - add new filter to `filter_manifest` method like on example:
|
56
|
+
|
57
|
+
> OLD Syntax
|
58
|
+
```ruby
|
59
|
+
class Job < ActiveRecord::Base
|
60
|
+
include RadCoreRails
|
61
|
+
|
62
|
+
def self.filter_manifest
|
63
|
+
{
|
64
|
+
zip_codes: ->(filter) { generate_zip_codes_clauses('addresses.zip', filter) }
|
65
|
+
}.with_indifferent_access
|
66
|
+
end
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
> NEW Syntax starting from v0.7.0
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class Job < ActiveRecord::Base
|
74
|
+
include RadCoreRails
|
75
|
+
|
76
|
+
filterable zip_codes: [:zip_codes, 'addresses.zip']
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
Step 5 - filter params example
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
{ key: 'zip_codes', option: '15', values: ['85001'] }
|
84
|
+
```
|
85
|
+
|
26
86
|
|
27
87
|
## Development
|
28
88
|
|
data/Rakefile
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require "rspec/core/rake_task"
|
3
|
+
require './support/active_record_rake_tasks'
|
3
4
|
|
4
5
|
RSpec::Core::RakeTask.new(:spec)
|
5
6
|
|
6
7
|
task :default => :spec
|
8
|
+
|
9
|
+
# Stub the :environment task for tasks like db:migrate & db:seed. Since this is a Gem we've explicitly required all
|
10
|
+
# dependent files in the needed places and we don't have to load the entire environment.
|
11
|
+
task :environment
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
|
2
|
+
def change
|
3
|
+
create_table :zip_codes do |t|
|
4
|
+
t.string :city
|
5
|
+
t.string :state
|
6
|
+
t.string :zip
|
7
|
+
t.decimal :latitude, precision: 10, scale: 6
|
8
|
+
t.decimal :longitude, precision: 10, scale: 6
|
9
|
+
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
|
13
|
+
add_index :zip_codes, :zip, unique: true
|
14
|
+
add_index :zip_codes, :latitude, unique: true
|
15
|
+
add_index :zip_codes, :longitude, unique: true
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "rails/generators/active_record"
|
2
|
+
|
3
|
+
module RadCoreRails
|
4
|
+
module Generators
|
5
|
+
class ZipCodesMigrationGenerator < Rails::Generators::Base
|
6
|
+
include ActiveRecord::Generators::Migration
|
7
|
+
source_root File.join(__dir__, "templates")
|
8
|
+
|
9
|
+
def copy_migration
|
10
|
+
migration_template "migration.rb", "db/migrate/create_zip_codes.rb", migration_version: migration_version
|
11
|
+
end
|
12
|
+
|
13
|
+
def migration_version
|
14
|
+
"[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/rad_core_rails.rb
CHANGED
@@ -1,15 +1,18 @@
|
|
1
|
-
|
2
|
-
require "active_support/concern"
|
3
|
-
require "active_support/core_ext"
|
1
|
+
# Need some refactoring: https://ankane.org/gem-patterns
|
4
2
|
require "active_record"
|
5
|
-
|
6
|
-
require
|
7
|
-
require
|
3
|
+
|
4
|
+
require "rad_core_rails/railtie" if defined?(Rails)
|
5
|
+
require "rad_core_rails/version"
|
6
|
+
require "rad_core_rails/query_generator"
|
7
|
+
require "rad_core_rails/search_terms"
|
8
|
+
require "rad_core_rails/sortable"
|
8
9
|
|
9
10
|
module RadCoreRails
|
11
|
+
autoload :ZipCode, 'rad_core_rails/zip_code'
|
12
|
+
|
10
13
|
def self.included(receiver)
|
11
14
|
receiver.send :include, Sortable, SearchTerms, QueryGenerator
|
12
15
|
end
|
13
16
|
|
14
17
|
class Error < StandardError; end
|
15
|
-
end
|
18
|
+
end
|
@@ -11,13 +11,17 @@ module RadCoreRails
|
|
11
11
|
@searchable_columns = handle_columns(columns)
|
12
12
|
end
|
13
13
|
|
14
|
+
def filterable(filters_data = [])
|
15
|
+
@filter_manifest = handle_filterable(filters_data)
|
16
|
+
end
|
17
|
+
|
14
18
|
def joins_clause
|
15
19
|
<<-SQL
|
16
20
|
SQL
|
17
21
|
end
|
18
22
|
|
19
23
|
def filter_manifest
|
20
|
-
{}.with_indifferent_access
|
24
|
+
@filter_manifest || {}.with_indifferent_access
|
21
25
|
end
|
22
26
|
|
23
27
|
def create_filters(search, filters)
|
@@ -29,10 +33,14 @@ module RadCoreRails
|
|
29
33
|
args << arg
|
30
34
|
end
|
31
35
|
filters.map do |filter|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
+
begin
|
37
|
+
clause, arguments = filter_manifest[filter[:key]].call(filter)
|
38
|
+
query << clause
|
39
|
+
arguments.each do |arg|
|
40
|
+
args << arg
|
41
|
+
end
|
42
|
+
rescue NoMethodError
|
43
|
+
raise NoMethodError.new("Filter with the name `#{filter[:key]}` doesn't exist.")
|
36
44
|
end
|
37
45
|
end
|
38
46
|
[query.reject(&:blank?).join(' AND '), args]
|
@@ -148,6 +156,13 @@ module RadCoreRails
|
|
148
156
|
[str, args]
|
149
157
|
end
|
150
158
|
|
159
|
+
def generate_zip_codes_clause(column_name, filter)
|
160
|
+
str = "(#{column_name} = ANY(#{zip_code_class.distance_query}))"
|
161
|
+
args = zip_code_class.distance_args(filter)
|
162
|
+
|
163
|
+
[str, args]
|
164
|
+
end
|
165
|
+
|
151
166
|
def generate_array_clause(column_name, filter)
|
152
167
|
str = "(#{column_name} && ARRAY[?])"
|
153
168
|
args = [filter[:values]]
|
@@ -236,6 +251,27 @@ module RadCoreRails
|
|
236
251
|
end
|
237
252
|
end
|
238
253
|
end
|
254
|
+
|
255
|
+
def handle_filterable(filters_data)
|
256
|
+
return nil if filters_data.empty?
|
257
|
+
@zip_code_class = filters_data.delete(:zip_code_class)
|
258
|
+
|
259
|
+
result = {}
|
260
|
+
|
261
|
+
filters_data.each do |filter_data|
|
262
|
+
filter_type, filter_column = filter_data.last
|
263
|
+
result[filter_data.first] = lambda { |filter|
|
264
|
+
public_send("generate_#{filter_type}_clause", filter_column, filter)
|
265
|
+
}
|
266
|
+
end
|
267
|
+
|
268
|
+
result.with_indifferent_access
|
269
|
+
end
|
270
|
+
|
271
|
+
def zip_code_class
|
272
|
+
return RadCoreRails::ZipCode unless @zip_code_class
|
273
|
+
@zip_code_class.to_s.constantize
|
274
|
+
end
|
239
275
|
end
|
240
276
|
end
|
241
277
|
end
|
@@ -1,31 +1,31 @@
|
|
1
|
-
module RadCoreRails
|
2
|
-
module SearchTerms
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
class_methods do
|
6
|
-
def _terms(column, search = '')
|
7
|
-
if search.present?
|
8
|
-
if column.is_a?(Array)
|
9
|
-
terms = []
|
10
|
-
search.split(' ').each do |term|
|
11
|
-
columns = []
|
12
|
-
column.each do |col|
|
13
|
-
columns.push("(replace(lower(#{Arel.sql(col)}), ' ', '') ILIKE '%#{Arel.sql(term.downcase)}%')")
|
14
|
-
end
|
15
|
-
terms.push '(' + columns.join(' OR ') + ')'
|
16
|
-
end
|
17
|
-
'(' + terms.join(' AND ') + ')'
|
18
|
-
else
|
19
|
-
terms = []
|
20
|
-
search.split(' ').each do |term|
|
21
|
-
terms.push("(replace(lower(#{Arel.sql(column)}), ' ', '') ILIKE '%#{Arel.sql(term.downcase)}%')")
|
22
|
-
end
|
23
|
-
'(' + terms.join(' AND ') + ')'
|
24
|
-
end
|
25
|
-
else
|
26
|
-
'1 = 1'
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
1
|
+
module RadCoreRails
|
2
|
+
module SearchTerms
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
def _terms(column, search = '')
|
7
|
+
if search.present?
|
8
|
+
if column.is_a?(Array)
|
9
|
+
terms = []
|
10
|
+
search.split(' ').each do |term|
|
11
|
+
columns = []
|
12
|
+
column.each do |col|
|
13
|
+
columns.push("(replace(lower(#{Arel.sql(col)}), ' ', '') ILIKE '%#{Arel.sql(term.downcase)}%')")
|
14
|
+
end
|
15
|
+
terms.push '(' + columns.join(' OR ') + ')'
|
16
|
+
end
|
17
|
+
'(' + terms.join(' AND ') + ')'
|
18
|
+
else
|
19
|
+
terms = []
|
20
|
+
search.split(' ').each do |term|
|
21
|
+
terms.push("(replace(lower(#{Arel.sql(column)}), ' ', '') ILIKE '%#{Arel.sql(term.downcase)}%')")
|
22
|
+
end
|
23
|
+
'(' + terms.join(' AND ') + ')'
|
24
|
+
end
|
25
|
+
else
|
26
|
+
'1 = 1'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
31
|
end
|
@@ -1,14 +1,14 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
module RadCoreRails
|
3
|
-
module Sortable
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
included do
|
7
|
-
scope :sort_order, ->(column, direction) { order(self.sort_column(column, direction)) }
|
8
|
-
|
9
|
-
def self.sort_column(column, direction)
|
10
|
-
"#{Arel.sql(column)} #{sort_direction(direction)}"
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module RadCoreRails
|
3
|
+
module Sortable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
scope :sort_order, ->(column, direction) { order(self.sort_column(column, direction)) }
|
8
|
+
|
9
|
+
def self.sort_column(column, direction)
|
10
|
+
"#{Arel.sql(column)} #{sort_direction(direction)}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module RadCoreRails
|
2
|
+
class ZipCode < ActiveRecord::Base
|
3
|
+
self.table_name = 'zip_codes'
|
4
|
+
|
5
|
+
validates_presence_of :city, :state, :zip, :latitude, :longitude
|
6
|
+
|
7
|
+
class << self
|
8
|
+
# Query for returning a one latitude for given zip-code
|
9
|
+
def lat_sub_query
|
10
|
+
<<-SQL
|
11
|
+
SELECT zip_codes.latitude
|
12
|
+
FROM zip_codes
|
13
|
+
WHERE zip = ?
|
14
|
+
LIMIT 1
|
15
|
+
SQL
|
16
|
+
end
|
17
|
+
|
18
|
+
# Query for returning a one longitude value for given zip-code
|
19
|
+
def lng_sub_query
|
20
|
+
<<-SQL
|
21
|
+
SELECT zip_codes.longitude
|
22
|
+
FROM zip_codes
|
23
|
+
WHERE zip = ?
|
24
|
+
LIMIT 1
|
25
|
+
SQL
|
26
|
+
end
|
27
|
+
|
28
|
+
# Query for returning an array of zip codes around the zip-code sent to this function with a given radius in miles.
|
29
|
+
def distance_query
|
30
|
+
<<-SQL
|
31
|
+
WITH t AS (SELECT
|
32
|
+
zip, (
|
33
|
+
3959 * acos (
|
34
|
+
cos(RADIANS((#{lat_sub_query})))
|
35
|
+
* cos(RADIANS(zip_codes.latitude))
|
36
|
+
* cos(RADIANS(zip_codes.longitude) - RADIANS((#{lng_sub_query})))
|
37
|
+
+ sin(RADIANS((#{lat_sub_query})))
|
38
|
+
* sin(RADIANS(zip_codes.latitude))
|
39
|
+
)
|
40
|
+
) AS distance
|
41
|
+
FROM zip_codes) SELECT t.zip FROM t WHERE t.distance < ?
|
42
|
+
SQL
|
43
|
+
end
|
44
|
+
|
45
|
+
# Query arguments for the `distance_query` based on passed filter params.
|
46
|
+
def distance_args(filter)
|
47
|
+
zip_value = filter[:values].try(:first) || ''
|
48
|
+
distance_miles = filter[:option] || 0
|
49
|
+
|
50
|
+
[zip_value, zip_value, zip_value, distance_miles]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "rad_core_rails/zip_code"
|
2
|
+
|
3
|
+
module RadCoreRails
|
4
|
+
class ZipImporter
|
5
|
+
attr_reader :file_path, :zip_codes_data
|
6
|
+
|
7
|
+
def initialize(file_path)
|
8
|
+
@file_path = file_path
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
puts "Parsing file..."
|
13
|
+
parse_data
|
14
|
+
puts "Parsing completed."
|
15
|
+
|
16
|
+
puts "Importing data..."
|
17
|
+
import_data!
|
18
|
+
puts "Successfully imported!"
|
19
|
+
|
20
|
+
true
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def import_data!
|
26
|
+
zip_codes_data.each do |zip_code_data|
|
27
|
+
fields = zip_code_data["fields"]
|
28
|
+
|
29
|
+
RadCoreRails::ZipCode.find_or_create_by!(
|
30
|
+
city: fields["city"].to_s,
|
31
|
+
state: fields["state"].to_s,
|
32
|
+
zip: fields["zip"].to_s,
|
33
|
+
latitude: fields["latitude"].to_s,
|
34
|
+
longitude: fields["longitude"].to_s
|
35
|
+
)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_data
|
40
|
+
@zip_codes_data = JSON.parse(IO.read(file_path))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
[
|
2
|
+
{
|
3
|
+
"datasetid": "us-zip-code-latitude-and-longitude",
|
4
|
+
"recordid": "9656735f9d9c7b64eae1f3f19c263b9598e057de",
|
5
|
+
"fields":
|
6
|
+
{
|
7
|
+
"city": "Eudora",
|
8
|
+
"zip": "66025",
|
9
|
+
"dst": 1,
|
10
|
+
"geopoint": [
|
11
|
+
38.917032, -95.06455
|
12
|
+
],
|
13
|
+
"longitude": -95.06455,
|
14
|
+
"state": "KS",
|
15
|
+
"latitude": 38.917032,
|
16
|
+
"timezone": -6
|
17
|
+
},
|
18
|
+
"geometry":
|
19
|
+
{
|
20
|
+
"type": "Point",
|
21
|
+
"coordinates": [
|
22
|
+
-95.06455, 38.917032
|
23
|
+
]
|
24
|
+
},
|
25
|
+
"record_timestamp": "2018-02-09T18:33:38.603+02:00"
|
26
|
+
},
|
27
|
+
{
|
28
|
+
"datasetid": "us-zip-code-latitude-and-longitude",
|
29
|
+
"recordid": "d4fd79372fc6ba5d7205ff4a430b0dcbe0c7dbbd",
|
30
|
+
"fields":
|
31
|
+
{
|
32
|
+
"city": "Savanna",
|
33
|
+
"zip": "74565",
|
34
|
+
"dst": 1,
|
35
|
+
"geopoint": [
|
36
|
+
34.831398, -95.83967
|
37
|
+
],
|
38
|
+
"longitude": -95.83967,
|
39
|
+
"state": "OK",
|
40
|
+
"latitude": 34.831398,
|
41
|
+
"timezone": -6
|
42
|
+
},
|
43
|
+
"geometry":
|
44
|
+
{
|
45
|
+
"type": "Point",
|
46
|
+
"coordinates": [
|
47
|
+
-95.83967, 34.831398
|
48
|
+
]
|
49
|
+
},
|
50
|
+
"record_timestamp": "2018-02-09T18:33:38.603+02:00"
|
51
|
+
},
|
52
|
+
{
|
53
|
+
"datasetid": "us-zip-code-latitude-and-longitude",
|
54
|
+
"recordid": "66439c8a6e1286118738eae874a5b0d1ef4dd249",
|
55
|
+
"fields":
|
56
|
+
{
|
57
|
+
"city": "Beckville",
|
58
|
+
"zip": "75631",
|
59
|
+
"dst": 1,
|
60
|
+
"geopoint": [
|
61
|
+
32.237924, -94.46427
|
62
|
+
],
|
63
|
+
"longitude": -94.46427,
|
64
|
+
"state": "TX",
|
65
|
+
"latitude": 32.237924,
|
66
|
+
"timezone": -6
|
67
|
+
},
|
68
|
+
"geometry":
|
69
|
+
{
|
70
|
+
"type": "Point",
|
71
|
+
"coordinates": [
|
72
|
+
-94.46427, 32.237924
|
73
|
+
]
|
74
|
+
},
|
75
|
+
"record_timestamp": "2018-02-09T18:33:38.603+02:00"
|
76
|
+
},
|
77
|
+
{
|
78
|
+
"datasetid": "us-zip-code-latitude-and-longitude",
|
79
|
+
"recordid": "0721c46587455339063b0b8e58766ddddfc2d83a",
|
80
|
+
"fields":
|
81
|
+
{
|
82
|
+
"city": "Rancho Santa Fe",
|
83
|
+
"zip": "92067",
|
84
|
+
"dst": 1,
|
85
|
+
"geopoint": [
|
86
|
+
33.016492, -117.20264
|
87
|
+
],
|
88
|
+
"longitude": -117.20264,
|
89
|
+
"state": "CA",
|
90
|
+
"latitude": 33.016492,
|
91
|
+
"timezone": -8
|
92
|
+
},
|
93
|
+
"geometry":
|
94
|
+
{
|
95
|
+
"type": "Point",
|
96
|
+
"coordinates": [
|
97
|
+
-117.20264, 33.016492
|
98
|
+
]
|
99
|
+
},
|
100
|
+
"record_timestamp": "2018-02-09T18:33:38.603+02:00"
|
101
|
+
}
|
102
|
+
]
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "rad_core_rails/zip_importer"
|
2
|
+
|
3
|
+
namespace :rad_core_rails do
|
4
|
+
desc "Import zip codes data from file"
|
5
|
+
task :import_zip_codes, [:file_path] => [:environment] do |task, args|
|
6
|
+
file_path = args[:file_path] || "#{File.dirname __dir__}/resources/us_zip_codes.json"
|
7
|
+
|
8
|
+
unless ActiveRecord::Base.connection.table_exists? "zip_codes"
|
9
|
+
abort "Table `zip_codes` doesn't exist. Run `rails generate rad_core_rails:zip_codes_migration`"
|
10
|
+
end
|
11
|
+
|
12
|
+
RadCoreRails::ZipImporter.new(file_path).call
|
13
|
+
end
|
14
|
+
end
|
data/rad_core_rails.gemspec
CHANGED
@@ -21,19 +21,24 @@ Gem::Specification.new do |spec|
|
|
21
21
|
# Specify which files should be added to the gem when it is released.
|
22
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
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)/}) }
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|support|bin|config|db)/}) }
|
25
25
|
end
|
26
26
|
spec.bindir = "exe"
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = ["lib"]
|
29
29
|
|
30
|
+
# spec.add_dependency "rails", '>= 4.2'
|
30
31
|
spec.add_dependency "activesupport", '>= 4.2'
|
31
32
|
spec.add_dependency "activemodel", '>= 4.2'
|
32
33
|
spec.add_dependency "activerecord", '>= 4.2'
|
33
|
-
|
34
|
+
|
34
35
|
spec.add_development_dependency "actionpack", '>= 4.2'
|
35
36
|
spec.add_development_dependency "bundler", '>= 1.17.3'
|
36
37
|
spec.add_development_dependency "rake"
|
37
|
-
spec.add_development_dependency "rspec"
|
38
|
+
spec.add_development_dependency "rspec-rails"
|
39
|
+
spec.add_development_dependency "factory_bot_rails"
|
40
|
+
spec.add_development_dependency "shoulda-matchers", '>= 4.0'
|
41
|
+
spec.add_development_dependency 'pg', '>= 0.19'
|
42
|
+
spec.add_development_dependency 'railties', '>= 5'
|
38
43
|
# spec.add_development_dependency "simplecov", '~> 0.17', '>= 0.18.5'
|
39
44
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rad_core_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleksandr Poltavets, James Marrs
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -95,19 +95,75 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name: rspec
|
98
|
+
name: rspec-rails
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: factory_bot_rails
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: shoulda-matchers
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '4.0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '4.0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: pg
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0.19'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0.19'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: railties
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '5'
|
104
160
|
type: :development
|
105
161
|
prerelease: false
|
106
162
|
version_requirements: !ruby/object:Gem::Requirement
|
107
163
|
requirements:
|
108
164
|
- - ">="
|
109
165
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
166
|
+
version: '5'
|
111
167
|
description: RadCoreRails lib
|
112
168
|
email:
|
113
169
|
- sancho.ck@gmail.com
|
@@ -117,17 +173,25 @@ extra_rdoc_files: []
|
|
117
173
|
files:
|
118
174
|
- ".gitignore"
|
119
175
|
- ".rspec"
|
176
|
+
- ".rubocop.yml"
|
120
177
|
- ".travis.yml"
|
121
178
|
- CODE_OF_CONDUCT.md
|
122
179
|
- Gemfile
|
123
180
|
- LICENSE.txt
|
124
181
|
- README.md
|
125
182
|
- Rakefile
|
183
|
+
- lib/generators/rad_core_rails/templates/migration.rb.tt
|
184
|
+
- lib/generators/rad_core_rails/zip_codes_migration_generator.rb
|
126
185
|
- lib/rad_core_rails.rb
|
127
186
|
- lib/rad_core_rails/query_generator.rb
|
187
|
+
- lib/rad_core_rails/railtie.rb
|
128
188
|
- lib/rad_core_rails/search_terms.rb
|
129
189
|
- lib/rad_core_rails/sortable.rb
|
130
190
|
- lib/rad_core_rails/version.rb
|
191
|
+
- lib/rad_core_rails/zip_code.rb
|
192
|
+
- lib/rad_core_rails/zip_importer.rb
|
193
|
+
- lib/resources/us_zip_codes.json
|
194
|
+
- lib/tasks/rad_core_rails.rake
|
131
195
|
- rad_core_rails.gemspec
|
132
196
|
homepage: https://github.com/jamesmarrs/rad-core-rails
|
133
197
|
licenses:
|
@@ -137,7 +201,7 @@ metadata:
|
|
137
201
|
homepage_uri: https://github.com/jamesmarrs/rad-core-rails
|
138
202
|
source_code_uri: https://rubygems.org/gems/rad_core_rails
|
139
203
|
changelog_uri: https://rubygems.org/gems/rad_core_rails
|
140
|
-
post_install_message:
|
204
|
+
post_install_message:
|
141
205
|
rdoc_options: []
|
142
206
|
require_paths:
|
143
207
|
- lib
|
@@ -153,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
217
|
version: '0'
|
154
218
|
requirements: []
|
155
219
|
rubygems_version: 3.1.2
|
156
|
-
signing_key:
|
220
|
+
signing_key:
|
157
221
|
specification_version: 4
|
158
222
|
summary: RadCoreRails
|
159
223
|
test_files: []
|