rad_core_rails 0.5.2 → 0.7.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 478fee8c5de287a633bff51aee69953c36f928392d6c1ac283e093f963e3733f
4
- data.tar.gz: 9a09f3052e99bb2c108093771fa2aca71845463ca9902e80bef82e586817bcac
3
+ metadata.gz: 60ce9f471b0a5bdbe4852663368a80b70f1b5962ed05b97f5d46f7c6efd186b6
4
+ data.tar.gz: 0ca944b3cf5d4ce272e7e0607ca202ba5af6672bf1107f29c55bf17c220d7803
5
5
  SHA512:
6
- metadata.gz: e01c9c2a528bb6819bd2857aed24c2eb560ba6be63498a62c68a21a7ea820bcb55ca75aca7d6ea1e6828f479ba9dad81a69427ce2ca079317bbbc860f2977bb8
7
- data.tar.gz: 8f42802719401dfdc5c0a9b3e90eef16f2e81e5258a4e25fbd10453f931fb320129c256974f6f63fb4ec9e6f8ff0f73bd5a2470bd315d34eeead9839de287893
6
+ metadata.gz: 5db7400ccea177cf7faed4899187c6da5ad98b2b06dbe9982f9696d5cfe8463b488a32c2df0f4410dfc460d7f941d357004f60bc4eb0b150a46a16d27e055f07
7
+ data.tar.gz: 1a82d4f0afad3f3138ca2901c39767bf69dadee7bb772d74d522eafc5baedcbbd436f53b50c99d78144c72d692fe88377f6beb38d62eff749efbcc2d5489d648
data/.gitignore CHANGED
@@ -8,9 +8,10 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  Gemfile.lock
11
- bin
11
+ /bin/
12
12
  *.gem
13
13
  .rspec_status
14
14
  .ruby-gemset
15
15
  .ruby-version
16
16
  /.byebug_history
17
+ .robocop.yml
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
@@ -0,0 +1,4 @@
1
+ AllCops:
2
+ SuggestExtensions: false
3
+ Style/FrozenStringLiteralComment:
4
+ Enabled: false
@@ -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
@@ -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/
@@ -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
- TODO: Write usage instructions here
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
15
+ add_index :zip_codes, :longitude
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
@@ -1,15 +1,18 @@
1
- require "rad_core_rails/version"
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
- require 'rad_core_rails/query_generator'
6
- require 'rad_core_rails/search_terms'
7
- require 'rad_core_rails/sortable'
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,31 +33,102 @@ module RadCoreRails
29
33
  args << arg
30
34
  end
31
35
  filters.map do |filter|
32
- clause, arguments = filter_manifest[filter[:key]].call(filter)
33
- query << clause
34
- arguments.each do |arg|
35
- args << arg
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]
39
47
  end
40
48
 
49
+ # def generate_search_clause(search)
50
+ # and_args = []
51
+ # or_args = []
52
+ # if search.present? && searchable_columns.is_a?(Array)
53
+ # # search_term_size = search.split(' ').length
54
+ # and_terms = search.split(' ').select { |term| !term.include?('+') }
55
+ # or_terms = search.split(' ').select { |term| term.include?('+') }
56
+ # and_columns = []
57
+ # or_columns = []
58
+ # # just_ors = search_term_size == or_term_size
59
+ # # operand = just_ors ? 'OR' : 'AND'
60
+ # # search_terms = just_ors == true ? search.split(' ') : search.split(' ').select { |term| !term.include?('+') }
61
+ # and_terms.each do |term|
62
+ # columns = []
63
+ # and_terms.each do |col|
64
+ # and_columns.push("(LOWER(#{col}) ILIKE ?)")
65
+ # and_args.push '%' + term.downcase.strip + '%'
66
+ # end
67
+ # or_terms.each do |col|
68
+ # columns.push("(LOWER(#{col}) ILIKE ?)")
69
+ # or_args.push '%' + term[1, term.length].downcase.strip + '%'
70
+ # end
71
+ # clause = if or_columns.empty?
72
+ # '(' + and_columns.join(' OR ') + ')'
73
+ # else
74
+ # '(' + '(' + and_columns.join(' OR ') + ')' + 'AND' + '(' + or_columns.join(' OR ') + ')' + ')'
75
+ # end
76
+ # end
77
+ # [clause, and_args + or_args]
78
+ # else
79
+ # ['', []]
80
+ # end
81
+ # end
82
+
83
+ # @searchable_columns, is an array of column names that you can compare to your terms.
84
+ #
41
85
  def generate_search_clause(search)
42
- args = []
43
86
  if search.present? && searchable_columns.is_a?(Array)
44
- search_term_size = search.split(' ').length
45
- or_term_size = search.split(' ').select { |term| term.include?('+') }.length
46
- operand = search_term_size == or_term_size ? 'OR' : 'AND'
47
- clause = []
48
- search.split(' ').each do |term|
87
+ # holds all of the sql from sanitized_and_terms
88
+ and_args = []
89
+ # holds all the table columns sql for all the AND terms
90
+ and_clause = []
91
+ # holds all of the sql from sanitized_or_terms
92
+ or_args = []
93
+ # holds all the table columns sql for all the OR terms
94
+ or_clause = []
95
+ # extract and terms, remove casing, and add ILIKE '%' comparisions
96
+ sanitized_and_terms = search.split(' ')
97
+ .reject { |term| term.include?('+') }
98
+ .map { |term| '%' + term.downcase.strip + '%' }
99
+ # extract or terms, remove casing, and add ILIKE '%' comparisions
100
+ sanitized_or_terms = search.split(' ')
101
+ .select { |term| term.include?('+') }
102
+ .map { |term| '%' + term[1, term.length].downcase.strip + '%' }
103
+ # loop through sanitized_and_terms to find all possible columns.
104
+ sanitized_and_terms.each do |sanitized_term|
105
+ columns = []
106
+ # all possible columns where this should be searched
107
+ searchable_columns.each do |col|
108
+ columns.push("(LOWER(#{col}) ILIKE ?)")
109
+ and_args.push sanitized_term
110
+ end
111
+ and_clause.push '(' + columns.join(' OR ') + ')'
112
+ end
113
+ # loop through sanitized_or_terms to find all possible columns.
114
+ sanitized_or_terms.each do |sanitized_term|
49
115
  columns = []
116
+ # all possible columns where this should be searched
50
117
  searchable_columns.each do |col|
51
118
  columns.push("(LOWER(#{col}) ILIKE ?)")
52
- args.push '%' + (operand == 'OR' ? term[1, term.length].downcase.strip : term.downcase.strip) + '%'
119
+ or_args.push sanitized_term
53
120
  end
54
- clause.push '(' + columns.join(' OR ') + ')'
121
+ or_clause.push '(' + columns.join(' OR ') + ')'
122
+ end
123
+ if or_clause.empty? && and_clause.empty?
124
+ ['', []]
125
+ elsif or_clause.empty?
126
+ ['(' + and_clause.join(' AND ') + ')', and_args]
127
+ elsif and_clause.empty?
128
+ ['(' + or_clause.join(' OR ') + ')', or_args]
129
+ else
130
+ ['((' + and_clause.join(' AND ') + ') AND (' + or_clause.join(' OR ') + '))', and_args + or_args]
55
131
  end
56
- ['(' + clause.join(" #{operand} ") + ')', args]
57
132
  else
58
133
  ['', []]
59
134
  end
@@ -68,13 +143,26 @@ module RadCoreRails
68
143
  [str, args]
69
144
  end
70
145
 
71
- def generate_model_clause(column_name, filter)
72
- str = "(#{column_name} #{filter[:option]} ANY(ARRAY[?]))"
146
+ def generate_model_clause(column_name, filter, optional_exclusion_clause = nil)
147
+ str = if optional_exclusion_clause.present? && filter[:option] == '!='
148
+ optional_exclusion_clause
149
+ else
150
+ # probably should fix this.
151
+ # somtimes we arent sending a filter option, so just default it to =
152
+ "(#{column_name} #{filter[:option] || '='} ANY(ARRAY[?]))"
153
+ end
73
154
  args = [filter[:values]]
74
155
 
75
156
  [str, args]
76
157
  end
77
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
+
78
166
  def generate_array_clause(column_name, filter)
79
167
  str = "(#{column_name} && ARRAY[?])"
80
168
  args = [filter[:values]]
@@ -163,6 +251,27 @@ module RadCoreRails
163
251
  end
164
252
  end
165
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
166
275
  end
167
276
  end
168
277
  end