filch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.travis.yml +7 -0
  4. data/CODE_OF_CONDUCT.md +74 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +303 -0
  8. data/Rakefile +10 -0
  9. data/app/controllers/filch_controller.rb +13 -0
  10. data/app/views/filch/_associations.html.haml +25 -0
  11. data/app/views/filch/_attributes.html.haml +30 -0
  12. data/app/views/filch/_distinct.html.haml +4 -0
  13. data/app/views/filch/_eq.html.haml +7 -0
  14. data/app/views/filch/_form.html.haml +26 -0
  15. data/app/views/filch/_group.html.haml +4 -0
  16. data/app/views/filch/_gt.html.haml +2 -0
  17. data/app/views/filch/_limit.html.haml +5 -0
  18. data/app/views/filch/_lt.html.haml +2 -0
  19. data/app/views/filch/_null.html.haml +5 -0
  20. data/app/views/filch/_options.html.haml +11 -0
  21. data/app/views/filch/_order.html.haml +4 -0
  22. data/app/views/filch/_pg_group.html.haml +6 -0
  23. data/app/views/filch/_pluck.html.haml +4 -0
  24. data/app/views/filch/_quick.html.haml +3 -0
  25. data/app/views/filch/_scopes.html.haml +11 -0
  26. data/app/views/filch/_search.html.haml +12 -0
  27. data/bin/console +14 -0
  28. data/bin/setup +8 -0
  29. data/config/initializers/assets.rb +1 -0
  30. data/config/initializers/ransack.rb +100 -0
  31. data/config/routes.rb +3 -0
  32. data/filch.gemspec +40 -0
  33. data/lib/filch.rb +375 -0
  34. data/lib/filch/datalist.rb +26 -0
  35. data/lib/filch/engine.rb +5 -0
  36. data/lib/filch/error.rb +4 -0
  37. data/lib/filch/model_find.rb +17 -0
  38. data/lib/filch/ransack_plus.rb +34 -0
  39. data/lib/filch/version.rb +3 -0
  40. data/vendor/assets/javascripts/filch.js +1 -0
  41. data/vendor/assets/javascripts/filchColClick.js +6 -0
  42. data/vendor/assets/javascripts/filchDataList.js +21 -0
  43. data/vendor/assets/javascripts/unhideForm.js +19 -0
  44. metadata +202 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 382178326c751023007e25da13e7e38c2347660a2bcf92f2cb11455188c623ca
4
+ data.tar.gz: 5913bc8b7f7eb753c46d4906e826d883e7e6e408751b5f15eccbadc81d56187e
5
+ SHA512:
6
+ metadata.gz: df89c51e20b4651a84045a86a2ea5dc56224016d503dff3968472b32aa71b56f599bfb90a358a0dda2d7b8e2784bc6d741d6f6c463e614e1b2644b61fb2c233d
7
+ data.tar.gz: 9a824e970edbe161361581892dd0035c3d88480703d83be6290d0b125035272d24a94a542ca9f7046fbaad9fff9ed7cfabe38eae70c1d06bd6d054a4dd0f237e
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
10
+ *.swp
11
+ .bundle
12
+ Gemfile.lock
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.1
7
+ before_install: gem install bundler -v 2.0.2
@@ -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 foo. 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
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'js_weber' # , path: '/home/x/backup/2019/js_weber/js_weber'
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 foo
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.
@@ -0,0 +1,303 @@
1
+ # Filch
2
+
3
+ Welcome to Filch!
4
+ Filch is an easy-to-use, customizable, advanced form builder for Ransack
5
+ With Filch, you just set up the template, Filch builds the form, and Ransack does the rest!
6
+ Ransack is a very useful search gem. See their github page.
7
+ ransack[https://github.com/activerecord-hackery/ransack]
8
+ At the very least, this can be a guide to some of Ransack's features.
9
+
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'filch'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install filch
26
+
27
+ ## Usage
28
+
29
+ ### Quick Usage
30
+ Filch requires ransack's <i>advanced search</i> routes to be setup
31
+
32
+ # config/routes.rb
33
+ resources :your_model do
34
+ collection do
35
+ match 'search' => 'your_model#your_controller_method', via: %i[get post], as: :search
36
+ end
37
+ end
38
+
39
+ In your controller:
40
+
41
+ @q = Filch.ransack_plus(YOUR_MODEL, (params[:q]))
42
+
43
+ And in your view:
44
+
45
+ =render('filch/filch', filch: Filch.templates(YOUR_MODEL), button_name: 'Search', results_p: search_YOURMODEL_path)
46
+
47
+ This will render a search button to show a search form with the default configuration.
48
+
49
+ ## Configuration
50
+
51
+ This gem is entirely built on the Ransack gem. Read the docs there.
52
+ ransack[https://github.com/activerecord-hackery/ransack]
53
+
54
+ At the very least this can be a useful guide for ransack, a very useful search gem.
55
+
56
+ Consider the following Model:
57
+
58
+ class HumanResource < ApplicationRecord
59
+ has_many :underlings, class_name: 'HumanResource', foreign_key 'boss'
60
+ belongs_to :boss, class_name: 'HumanResource', optional: true
61
+ scope :position, ->(title) { where(title: title) }
62
+ scope :retired, -> { where(retired: 1) }
63
+ end
64
+
65
+ HumanResource has the schema:
66
+
67
+ t.string "name"
68
+ t.string "title"
69
+ t.big_int "boss_id"
70
+ t.integer "retired"
71
+ t.string "skills", default: [], array: true
72
+
73
+ The Array <i>skills</i>, of course, requires a compatible database. Here we're using postgres.
74
+
75
+ Filch sees Ransack as implementing three kinds of querys.
76
+ * Attributes
77
+ ransackable_attributes are essentially the databases columns. In this case [:name, :title, :boss_id, :retired, :skills]
78
+ * Associations
79
+ ransackable_associations are the model associations. In this case [:boss]
80
+ * Scopes
81
+ ransackable_scopes are not automaticly defined, but need an extra method in your model.
82
+
83
+ Ransack uses Arel predicates to create its querys.
84
+ a list of Ransack predicates can be found here[]
85
+ a list of Arel predicates can be found here[]
86
+ custom predicates will be described later.
87
+
88
+ Filch uses templates to build the form(s) as desired. The tempaltes can be named whatever you wish, of course, but for this example we will use 'basic_template' and 'advanced'. References to an undefined template will use a default configuration.
89
+
90
+ ### Templates
91
+
92
+ Filch builds the forms off of user-defined methods in the model. Name the templates in a #templates method.
93
+
94
+ class HumanResource < ApplicationRecord
95
+ ...
96
+ def self.templates
97
+ %i[basic, all]
98
+ end
99
+ end
100
+
101
+ If more then one template is listed, Filch will provide a select tag to choose which form-template to show. Without any further configuration, Filch will build the templates with defaults.
102
+
103
+
104
+ ### Attributes
105
+
106
+ Ransack expects the query object name for an attribute to be in the following format: attribute_predicate, e.g.
107
+
108
+ f.search_field('name_eq')
109
+
110
+ Ransack will use this query to return every entry from the HumanResource model where name equals the given name.
111
+
112
+ Filch builds its forms using partials. The partial for the 'eq' predicate can found here[]. You can easily override these partials by creating your own. In this case the file app/views/filch/_eq.html.haml will override the default.
113
+ In the built-in '_eq.html.haml' you will notice the option {list: datalist_id} This links the dropdown datalist options to a datalist tag. More on this later.
114
+
115
+ Which attributes with which predicates Filch will show in the form is configurable in the model. By default it will show all attributes, with a default collection of predicates. To do this add a method to the model:
116
+
117
+ # arrays of which attributes to build
118
+ # this is kept as a separate array to allow predefined sorting
119
+ def self.filch_attrs
120
+ {
121
+ basic_template: %i[name title retired],
122
+ advanced: %i[name title boss_id retired]
123
+ }
124
+ end
125
+
126
+ def self.filch_attributes
127
+ {
128
+ basic_template: {
129
+ name: %w[eq cont distinct],
130
+ title: %w[eq]
131
+ }
132
+ }
133
+ end
134
+
135
+ ### Associations
136
+
137
+ Ransack expects the query object name for an association to be in the following format: association_attribute_predicate, e.g.
138
+
139
+ f.search_field('underlings_name_eq')
140
+
141
+ Ransack will use this query to return every entry from the HumanResource model where an underling's name equals the given name.
142
+
143
+ Filch will again use relevant partials as described above, in Attributes.
144
+
145
+ Which associations with which attiribute and predicates Filch will show in the form is confiugrable in the model, similar to Attributes.
146
+
147
+ def self.filch_assoc_keys
148
+ {
149
+ basic_template: %i[underlings boss]
150
+ }
151
+ end
152
+
153
+ # notice the nested hashes here
154
+ def self.filch_associations
155
+ {
156
+ basic_template: {
157
+ underlings: {
158
+ name: %w[eq]
159
+ }
160
+ }
161
+ }
162
+ end
163
+
164
+ ### Scopes
165
+
166
+ As mentioned earlier, Ransack requires ransackable_scopes to be defined in the model:
167
+
168
+ def self.ransackable_scope(auth_obj = nil)
169
+ %w[retired position limit order]
170
+ end
171
+
172
+ Scopes do not use a predicate, instead, Filch will by default use a check box, to decide if ransack should use the scope. This works great for scopes like retired. For a scope like postion that takes a parameter Filch will <b>require</b> the creation of a relevant partial.
173
+
174
+ app/views/filch/_position.html.haml:
175
+
176
+ %td
177
+ Postion
178
+ %td
179
+ =f.select(scope_name, %w[Clerk ShiftManager StoreManager Executive])
180
+
181
+ of course, you can user erb if you prefer.
182
+
183
+ Filch <b>does</b> have built-in order and limit partials. The order partial uses a select box, options are the model's columns, in this case: name, title, boss_id, retired, and skills. Limit is a number field, to limit the results. 0 returns all. 1 returns 2, as a workaround for Ransack's only returning one element instead of a collection when limited to 1. Limit and Order do not have to be defined on the model, as Rails created those methods for you.
184
+
185
+
186
+
187
+ ### Default Values
188
+
189
+ Default values per template can be assigned in the model.
190
+
191
+ def self.filch_defaults
192
+ {
193
+ basic: {
194
+ positon_eq: 'Clerk'
195
+ }
196
+ }
197
+ end
198
+
199
+ ### html attributes
200
+
201
+ Filch can be further customized by assigning html_attributes in the model. This is <b>not</b> per template, but rather will apply to all forms. For example, if you do not like that associations start off hidden, the javascript load 'toggleShowHide' can be removed as follows:
202
+
203
+ def self.filch_html_attrs
204
+ {
205
+ assocs: { load: nil }
206
+ }
207
+ end
208
+
209
+ If you do not want the datalists to update:
210
+
211
+ datalist: { class: 'off' }
212
+
213
+ The default html_attributes are merged with html_attributes assigned in the model.
214
+
215
+ Possible filch_html_attrs keys are:
216
+ attrs: defines the '<p>' tag starting each attribute
217
+ assocs: defines the '<p>' tag starting each association. See the example above.
218
+ button: defines the clickable button that show/hides filch.
219
+ datalist: defines the datalists.
220
+ field: defines the fields.
221
+ label: defines the '<td>' containing the field label.
222
+ search_form: defines the search_form.
223
+
224
+ ### Custom Ransackers and Custom Predicates
225
+
226
+ Ranack allows for the creation of custom ranasackers. Put them in 'config/initializers/ransack.rb'. They can get messy.
227
+
228
+ Filch has added three custom ransackers: like, array_cont, and array_not_cont
229
+
230
+ The like predicate is for postgres. By default the cont predicate in Arel uses ilike with postgres, which is case-insensitive. Nice of them, but case-insensitive is slow, so should be avoided when possible. A search_field using the like predicate would look like:
231
+
232
+ =f.text_field("#{attr}_like", {list: datalist_id, class: 'js', input: "filchDataList"})
233
+
234
+ Of course, a Filch configuration referencing a like predicate will do this automaticly.
235
+
236
+ The array_cont and array_not_cont predicates were similarly build for postgres. They may work for other databases with an array type. The predicate array_cont will translate to
237
+
238
+ WHERE foo = ANY(bar);
239
+
240
+ The array_not_cont will translate to:
241
+
242
+ WHERE foo DOES NOT CONT All(bar);
243
+
244
+
245
+
246
+ ### Datalist
247
+
248
+ A Filch form features datalists to create a dropdown list of options in the form element. The datalist does not require any configuration. Filch uses javascript to rebuild the datalist on-the-fly. If you want the datalist in your custom form element, add the list option to your helper. As an example, from the built-in search partial:
249
+
250
+ =f.text_field("#{attr}", {list: datalist_id, class: 'js', input: "filchDataList"})
251
+
252
+ attr and datalist_id are passed to your partial from the built-in form partial. attr is the name of the attribute. datalist_id is the id of the relevant datalist. If you do not want the datalist to change 'onInput' (e.g. for an eq predicate, where updating as you type would be foolish) remove the input option. If you do not want the datalist (e.g. for a select box, or a number) remove all the options. Updates are turned off while waiting for the results, to keep from hammering the server.
253
+
254
+ The speed of the updates depends of course on the size of the database and the quality of the indexes.
255
+
256
+
257
+ ### Results
258
+
259
+ With Ransack the controller looks something like this:
260
+
261
+ def search
262
+ @q = YOUR_MODEL.ransack(params[:q])
263
+ @results = @q.result(@q)
264
+ end
265
+
266
+ With Filch the controller looks something like:
267
+
268
+ def search
269
+ filch = Filch.ransack_plus(YOUR_MODEL, params[:q])
270
+ @q = filch.q
271
+ @results = filch.results
272
+ end
273
+
274
+ Alternatively, if you don't need filch's ransack_plus results:
275
+
276
+ def search
277
+ filch = Filch.ransack_plus(YOUR_MODEL, params[:q])
278
+ @q = filch.q
279
+ @results = @q.result
280
+ end
281
+
282
+ Ransack plus adds methods for workarounds
283
+
284
+ limit: when using limit as a scope, submitting '1' returns true and '0' false. Which is no good as the scope requires an argument. So '1' gets turned into '2' and '0' deletes the parameter, removing the limit scope, and will return all results.
285
+
286
+
287
+ ## Development
288
+
289
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
290
+
291
+ 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).
292
+
293
+ ## Contributing
294
+
295
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/filch. 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.
296
+
297
+ ## License
298
+
299
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
300
+
301
+ ## Code of Conduct
302
+
303
+ Everyone interacting in the Filch project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/filch/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test