administrate-field-lazy_belongs_to 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a6af0d252d532323cb506524d73fa060e83ae40ad7d2ac54ef5e402cb596d128
4
+ data.tar.gz: ef6b9e1cd805114f87080be3025d853f04acc81b7907f688ed76a8be1ab8f9e1
5
+ SHA512:
6
+ metadata.gz: 9c5ed7524305bedfe7a46f07298830842dcb4aed3e31c85cc7efd097ded09e2b9c0f2359b76dcd0d2ff0bcfd6bdfba43c62de4de988463d78c4aee054d0857c2
7
+ data.tar.gz: 5dc7ba9b932bd07bec785c52bc688ad6189a86d61885c4a3336082df9e93a49bbb313391099576cbdc67412aa3700bff3ce7aec7cbe139d8af35dfd8e4e2e7bc
data/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .idea/workspace.xml
12
+ .idea/usage.statistics.xml
13
+ .idea/tasks.xml
14
+
15
+ .idea/dataSources**
16
+ .idea/dataSources.local.xml
data/.idea/.rakeTasks ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build administrate-field-hyperlink-0.1.0.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Build and install administrate-field-hyperlink-0.1.0.gem into system gems" fullCmd="install" taksId="install" /><RakeGroup description="" fullCmd="" taksId="install"><RakeTask description="Build and install administrate-field-hyperlink-0.1.0.gem into system gems without network access" fullCmd="install:local" taksId="local" /></RakeGroup><RakeTask description="Create tag v0.1.0 and build and push administrate-field-hyperlink-0.1.0.gem to rubygems.org" fullCmd="release[remote]" taksId="release[remote]" /><RakeTask description="Run tests" fullCmd="test" taksId="test" /><RakeTask description="" fullCmd="default" taksId="default" /><RakeTask description="" fullCmd="release" taksId="release" /><RakeGroup description="" fullCmd="" taksId="release"><RakeTask description="" fullCmd="release:guard_clean" taksId="guard_clean" /><RakeTask description="" fullCmd="release:rubygem_push" taksId="rubygem_push" /><RakeTask description="" fullCmd="release:source_control_push" taksId="source_control_push" /></RakeGroup></RakeGroup></Settings>
@@ -0,0 +1,70 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$" />
8
+ <orderEntry type="inheritedJdk" />
9
+ <orderEntry type="sourceFolder" forTests="false" />
10
+ <orderEntry type="library" scope="PROVIDED" name="actioncable (v5.2.2.1, ruby-2.5.3-p105) [gem]" level="application" />
11
+ <orderEntry type="library" scope="PROVIDED" name="actionmailer (v5.2.2.1, ruby-2.5.3-p105) [gem]" level="application" />
12
+ <orderEntry type="library" scope="PROVIDED" name="actionpack (v5.2.2.1, ruby-2.5.3-p105) [gem]" level="application" />
13
+ <orderEntry type="library" scope="PROVIDED" name="actionview (v5.2.2.1, ruby-2.5.3-p105) [gem]" level="application" />
14
+ <orderEntry type="library" scope="PROVIDED" name="activejob (v5.2.2.1, ruby-2.5.3-p105) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="activemodel (v5.2.2.1, ruby-2.5.3-p105) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="activerecord (v5.2.2.1, ruby-2.5.3-p105) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="activestorage (v5.2.2.1, ruby-2.5.3-p105) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="activesupport (v5.2.2.1, ruby-2.5.3-p105) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="administrate (v0.11.0, ruby-2.5.3-p105) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="arel (v9.0.0, ruby-2.5.3-p105) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="autoprefixer-rails (v9.5.0, ruby-2.5.3-p105) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="builder (v3.2.3, ruby-2.5.3-p105) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v2.0.1, ruby-2.5.3-p105) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="concurrent-ruby (v1.1.5, ruby-2.5.3-p105) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="crass (v1.0.4, ruby-2.5.3-p105) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="datetime_picker_rails (v0.0.7, ruby-2.5.3-p105) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="erubi (v1.8.0, ruby-2.5.3-p105) [gem]" level="application" />
28
+ <orderEntry type="library" scope="PROVIDED" name="execjs (v2.7.0, ruby-2.5.3-p105) [gem]" level="application" />
29
+ <orderEntry type="library" scope="PROVIDED" name="ffi (v1.10.0, ruby-2.5.3-p105) [gem]" level="application" />
30
+ <orderEntry type="library" scope="PROVIDED" name="globalid (v0.4.2, ruby-2.5.3-p105) [gem]" level="application" />
31
+ <orderEntry type="library" scope="PROVIDED" name="i18n (v1.6.0, ruby-2.5.3-p105) [gem]" level="application" />
32
+ <orderEntry type="library" scope="PROVIDED" name="jquery-rails (v4.3.3, ruby-2.5.3-p105) [gem]" level="application" />
33
+ <orderEntry type="library" scope="PROVIDED" name="kaminari (v1.1.1, ruby-2.5.3-p105) [gem]" level="application" />
34
+ <orderEntry type="library" scope="PROVIDED" name="kaminari-actionview (v1.1.1, ruby-2.5.3-p105) [gem]" level="application" />
35
+ <orderEntry type="library" scope="PROVIDED" name="kaminari-activerecord (v1.1.1, ruby-2.5.3-p105) [gem]" level="application" />
36
+ <orderEntry type="library" scope="PROVIDED" name="kaminari-core (v1.1.1, ruby-2.5.3-p105) [gem]" level="application" />
37
+ <orderEntry type="library" scope="PROVIDED" name="loofah (v2.2.3, ruby-2.5.3-p105) [gem]" level="application" />
38
+ <orderEntry type="library" scope="PROVIDED" name="mail (v2.7.1, ruby-2.5.3-p105) [gem]" level="application" />
39
+ <orderEntry type="library" scope="PROVIDED" name="marcel (v0.3.3, ruby-2.5.3-p105) [gem]" level="application" />
40
+ <orderEntry type="library" scope="PROVIDED" name="method_source (v0.9.2, ruby-2.5.3-p105) [gem]" level="application" />
41
+ <orderEntry type="library" scope="PROVIDED" name="mimemagic (v0.3.3, ruby-2.5.3-p105) [gem]" level="application" />
42
+ <orderEntry type="library" scope="PROVIDED" name="mini_mime (v1.0.1, ruby-2.5.3-p105) [gem]" level="application" />
43
+ <orderEntry type="library" scope="PROVIDED" name="mini_portile2 (v2.4.0, ruby-2.5.3-p105) [gem]" level="application" />
44
+ <orderEntry type="library" scope="PROVIDED" name="minitest (v5.11.3, ruby-2.5.3-p105) [gem]" level="application" />
45
+ <orderEntry type="library" scope="PROVIDED" name="momentjs-rails (v2.20.1, ruby-2.5.3-p105) [gem]" level="application" />
46
+ <orderEntry type="library" scope="PROVIDED" name="nio4r (v2.3.1, ruby-2.5.3-p105) [gem]" level="application" />
47
+ <orderEntry type="library" scope="PROVIDED" name="nokogiri (v1.10.1, ruby-2.5.3-p105) [gem]" level="application" />
48
+ <orderEntry type="library" scope="PROVIDED" name="rack (v2.0.6, ruby-2.5.3-p105) [gem]" level="application" />
49
+ <orderEntry type="library" scope="PROVIDED" name="rack-test (v1.1.0, ruby-2.5.3-p105) [gem]" level="application" />
50
+ <orderEntry type="library" scope="PROVIDED" name="rails (v5.2.2.1, ruby-2.5.3-p105) [gem]" level="application" />
51
+ <orderEntry type="library" scope="PROVIDED" name="rails-dom-testing (v2.0.3, ruby-2.5.3-p105) [gem]" level="application" />
52
+ <orderEntry type="library" scope="PROVIDED" name="rails-html-sanitizer (v1.0.4, ruby-2.5.3-p105) [gem]" level="application" />
53
+ <orderEntry type="library" scope="PROVIDED" name="railties (v5.2.2.1, ruby-2.5.3-p105) [gem]" level="application" />
54
+ <orderEntry type="library" scope="PROVIDED" name="rake (v10.5.0, ruby-2.5.3-p105) [gem]" level="application" />
55
+ <orderEntry type="library" scope="PROVIDED" name="rb-fsevent (v0.10.3, ruby-2.5.3-p105) [gem]" level="application" />
56
+ <orderEntry type="library" scope="PROVIDED" name="rb-inotify (v0.10.0, ruby-2.5.3-p105) [gem]" level="application" />
57
+ <orderEntry type="library" scope="PROVIDED" name="sass (v3.7.3, ruby-2.5.3-p105) [gem]" level="application" />
58
+ <orderEntry type="library" scope="PROVIDED" name="sass-listen (v4.0.0, ruby-2.5.3-p105) [gem]" level="application" />
59
+ <orderEntry type="library" scope="PROVIDED" name="sass-rails (v5.0.7, ruby-2.5.3-p105) [gem]" level="application" />
60
+ <orderEntry type="library" scope="PROVIDED" name="selectize-rails (v0.12.6, ruby-2.5.3-p105) [gem]" level="application" />
61
+ <orderEntry type="library" scope="PROVIDED" name="sprockets (v3.7.2, ruby-2.5.3-p105) [gem]" level="application" />
62
+ <orderEntry type="library" scope="PROVIDED" name="sprockets-rails (v3.2.1, ruby-2.5.3-p105) [gem]" level="application" />
63
+ <orderEntry type="library" scope="PROVIDED" name="thor (v0.20.3, ruby-2.5.3-p105) [gem]" level="application" />
64
+ <orderEntry type="library" scope="PROVIDED" name="thread_safe (v0.3.6, ruby-2.5.3-p105) [gem]" level="application" />
65
+ <orderEntry type="library" scope="PROVIDED" name="tilt (v2.0.9, ruby-2.5.3-p105) [gem]" level="application" />
66
+ <orderEntry type="library" scope="PROVIDED" name="tzinfo (v1.2.5, ruby-2.5.3-p105) [gem]" level="application" />
67
+ <orderEntry type="library" scope="PROVIDED" name="websocket-driver (v0.7.0, ruby-2.5.3-p105) [gem]" level="application" />
68
+ <orderEntry type="library" scope="PROVIDED" name="websocket-extensions (v0.1.3, ruby-2.5.3-p105) [gem]" level="application" />
69
+ </component>
70
+ </module>
@@ -0,0 +1,6 @@
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ <inspection_tool class="JSUnresolvedVariable" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
5
+ </profile>
6
+ </component>
data/.idea/misc.xml ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptSettings">
4
+ <option name="languageLevel" value="ES6" />
5
+ </component>
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="ruby-2.5.3-p105" project-jdk-type="RUBY_SDK" />
7
+ </project>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/administrate-field-lazy_belongs_to.iml" filepath="$PROJECT_DIR$/.idea/administrate-field-lazy_belongs_to.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
data/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
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 install bundler -v 2.0.1
11
+ - gem update --system
12
+ - gem --version
13
+ matrix:
14
+ allow_failures:
15
+ - rvm: ruby-head
16
+ install:
17
+ - bundle install --with development --jobs=3 --retry=3 --path=${BUNDLE_PATH:-vendor/bundle}
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.1.0
4
+
5
+ :baby: Initial commit
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Derk-Jan Karrenbeld
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # Administrate::Field::LazyBelongsTo
2
+
3
+ [![Build Status: master](https://travis-ci.com/XPBytes/administrate-field-lazy_belongs_to.svg)](https://travis-ci.com/XPBytes/administrate-field-lazy_belongs_to)
4
+ [![Gem Version](https://badge.fury.io/rb/administrate-field-lazy_belongs_to.svg)](https://badge.fury.io/rb/administrate-field-lazy_belongs_to)
5
+ [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
6
+
7
+ An input field that shows search results of an association, lazily.
8
+
9
+ ![Example](./example.png)
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'administrate-field-lazy_belongs_to'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install administrate-field-lazy_belongs_to
26
+
27
+ ## Usage
28
+
29
+ This field needs a bit of setup, unless you're using [XPBytes/administrate-base_controller](https://github.com/XPBytes/administrate-base_controller),
30
+ in order to lazily collect the values you're looking for:
31
+
32
+ You need to have a route that yields an array of objects with at least the value and label you want to show. The best
33
+ way is to re-use everything administrate offers you, which is the default behaviour of `Administrate::BaseController`:
34
+
35
+ ```ruby
36
+ class BookController < Admin::ApplicationController
37
+ def index
38
+ respond_to do |format|
39
+ format.json do
40
+ resources = Administrate::Search.new(scoped_resource, dashboard_class, params[:search].to_s.strip).run
41
+ resources = apply_resource_includes(resources)
42
+ resources = order.apply(resources)
43
+ resources = resources.page(params[:page]).per(records_per_page)
44
+
45
+ render json: resources
46
+ end
47
+
48
+ format.any { super }
49
+ end
50
+ end
51
+ end
52
+ ```
53
+
54
+ This way, your already existing route also is available in JSON. You may optimize this by only use the id and value
55
+ field: `resources.to_json(fields: %i[id name])`.
56
+
57
+ The rest of the setup is as easy as any other field, except that you need to tell the field where to query the results,
58
+ which key it should use for the value (defaults to `id`) and the label (defaults to `name`).
59
+
60
+ ```ruby
61
+ require "administrate/base_dashboard"
62
+
63
+ class BookDashboard < Administrate::BaseDashboard
64
+ # ATTRIBUTE_TYPES
65
+ # a hash that describes the type of each of the model's fields.
66
+ #
67
+ # Each different type represents an Administrate::Field object,
68
+ # which determines how the attribute is displayed
69
+ # on pages throughout the dashboard.
70
+ ATTRIBUTE_TYPES = {
71
+ id: Field::String,
72
+ name: Field::String,
73
+ author: Field::LazyBelongsTo.with_options(
74
+ placeholder: 'Select the author',
75
+ action: ->(field, q:) { field.url_helpers.admin_publisher_books_path(field.resource.publisher, search: q) },
76
+ value_attribute: :id,
77
+ label_attribute: :name
78
+ ),
79
+ created_at: Field::DateTime,
80
+ updated_at: Field::DateTime
81
+ }.freeze
82
+
83
+ # ...
84
+ end
85
+ ```
86
+
87
+ You get the `field` and the search template `q` as argument and named argument. By default this is `{q}` which is
88
+ replaced with the actual query on the front-end. You can use the `field.resource` shortcut to scope the url; in this
89
+ example above it will only show authors with the current publisher.
90
+
91
+ ### Options
92
+
93
+ | option | type | default | description |
94
+ |--------|------|---------|-------------|
95
+ | `placeholder` | `string` | `'Select a %<associated_class>s'` | The button text if there is no current value |
96
+ | `action` | `lambda -> (field, q: string)` | | The path to get the associated |
97
+ | `value_attribute` | `string` or `symbol` | `id` | The attribute that you want to store in the field (`association_id`) |
98
+ | `label_attribute` | `string` or `symbol` | `name` | The attribute that you want to show to the user (label) |
99
+ | `size` | `number` | `10` | The number of results to show at most (the rest will scroll) |
100
+
101
+ ## Related
102
+
103
+ - [`Administrate`](https://github.com/thoughtbot/administrate): A Rails engine that helps you put together a super-flexible admin dashboard.
104
+ - [`Administrate::BaseController`](https://github.com/XPBytes/administrate-base_controller): :stars: A set of application controller improvements.
105
+
106
+ ### Concerns
107
+
108
+ - [`Administrate::DefaultOrder`](https://github.com/XPBytes/administrate-default_order): :1234: Sets the default order for a resource in a administrate controller.
109
+ - [`Administrate::SerializedFields`](https://github.com/XPBytes/administrate-serialized_fields): :ab: Automatically deserialize administrate fields on form submit.
110
+
111
+ ### Fields
112
+
113
+ - [`Administrate::Field::Hyperlink`](https://github.com/XPBytes/administrate-field-hyperlink): :pencil: A `string` field that is shows a hyperlink.
114
+ - [`Adminisrtate::Field::JsonEditor`](https://github.com/XPBytes/administrate-field-json_editor): :pencil: A `text` field that shows a [JSON editor](https://github.com/josdejong/jsoneditor).
115
+ - [`Administrate::Field::ScopedBelongsTo`](https://github.com/XPBytes/administrate-field-scoped_belongs_to): :pencil: A `belongs_to` field that yields itself to the scope `lambda`.
116
+ - [`Administrate::Field::ScopedHasMany`](https://github.com/XPBytes/administrate-field-scoped_has_many): :pencil: A `has_many` field that yields itself to the scope `lambda`.
117
+ - [`Administrate::Field::TimeAgo`](https://github.com/XPBytes/administrate-field-time_ago): :pencil: A `date_time` field that shows its data as `time_ago` since.
118
+
119
+ ## Development
120
+
121
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can
122
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
123
+
124
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
125
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
126
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
127
+
128
+ ## Contributing
129
+
130
+ Bug reports and pull requests are welcome on GitHub at [XPBytes/administrate-field-lazy_belongs_to](https://github.com/XPBytes/administrate-field-lazy_belongs_to).
data/Rakefile ADDED
@@ -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
@@ -0,0 +1,42 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'administrate/field/lazy_belongs_to/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'administrate-field-lazy_belongs_to'
7
+ spec.version = Administrate::Field::LazyBelongsToVersion::VERSION
8
+ spec.authors = ['Derk-Jan Karrenbeld']
9
+ spec.email = ['derk-jan@xpbytes.com']
10
+
11
+ spec.summary = 'A belongs to-like field that lazily loads candidates from a custom endpoint.'
12
+ spec.homepage = 'https://github.com/XPBytes/administrate-field-lazy_belongs_to'
13
+ spec.license = 'MIT'
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ # spec.metadata['allowed_push_host'] = 'TODO: Set to 'http://mygemserver.com''
19
+
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ spec.metadata['source_code_uri'] = spec.homepage
22
+ spec.metadata['changelog_uri'] = spec.homepage + '/CHANGELOG.md'
23
+ else
24
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
25
+ 'public gem pushes.'
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = 'exe'
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ['lib']
36
+
37
+ spec.add_development_dependency 'administrate', '>= 0.11.0', '< 1'
38
+ spec.add_development_dependency 'bundler', '~> 2.0'
39
+ spec.add_development_dependency 'minitest', '~> 5.0'
40
+ spec.add_development_dependency 'rails'
41
+ spec.add_development_dependency 'rake', '~> 10.0'
42
+ end
@@ -0,0 +1,2 @@
1
+ //= require_tree .
2
+ //= require_self
@@ -0,0 +1,106 @@
1
+ function bindLazyBelongsTos() {
2
+ const lazySelects = document.querySelectorAll('[data-component="lazy-belongs-to"]')
3
+
4
+ lazySelects.forEach(lazySelect => {
5
+ const target = lazySelect.querySelector('input[type="hidden"]')
6
+ const input = lazySelect.querySelector('input[type="search"]')
7
+ const button = lazySelect.querySelector('button[type="button"]')
8
+ const popout = lazySelect.querySelector('[data-target="popout"]')
9
+ const output = lazySelect.querySelector('[data-target="output"]')
10
+ const select = output.querySelector('select')
11
+
12
+ const options = JSON.parse(lazySelect.getAttribute('data-lazy-belongs-to'))
13
+
14
+ let controller = undefined
15
+ let lastResult = undefined
16
+
17
+ function onQuery (event) {
18
+ const value = event.currentTarget.value
19
+
20
+ if (controller) {
21
+ // abort the previous request
22
+ controller.abort()
23
+ }
24
+
25
+ controller = new AbortController()
26
+ const { signal } = controller
27
+ console.log({ hidden: target.value, selected: select.value, query: value })
28
+
29
+ fetch(options.url.replace('{q}', value).replace('%7Bq%7D', value), {
30
+ signal,
31
+ headers: { Accept: 'application/json' }
32
+ })
33
+ .then(r => r.json())
34
+ .then(r => r.map(e => ({ value: e[options.value], label: e[options.label] })))
35
+ .then(rs => {
36
+ const currentResult = JSON.stringify((rs))
37
+
38
+ if (lastResult && lastResult === currentResult) {
39
+ return
40
+ }
41
+
42
+ lastResult = currentResult
43
+
44
+ while (select.lastChild) {
45
+ select.removeChild(select.lastChild);
46
+ }
47
+
48
+ const currentValue = target.value
49
+
50
+ rs.forEach(r => {
51
+ const option = document.createElement('option')
52
+ option.setAttribute('value', r.value)
53
+ option.innerText = r.label
54
+ option.selected = currentValue === r.value
55
+ select.appendChild(option)
56
+ })
57
+
58
+ select.setAttribute('size', "" + Math.max(2, Math.min(Number(select.getAttribute('data-max-size')), rs.length)))
59
+ })
60
+ .catch(error => {
61
+ if (error.name === 'AbortError') {
62
+ return /* ignore, this is an aborted promise */
63
+ }
64
+ console.error(error)
65
+ })
66
+ }
67
+
68
+ function showPopout() {
69
+ popout.classList.add('active')
70
+ }
71
+
72
+ function hidePopout() {
73
+ popout.classList.remove('active')
74
+ }
75
+
76
+ input.addEventListener('input', onQuery)
77
+ button.addEventListener('click', showPopout)
78
+
79
+ document.addEventListener('click', (e) => {
80
+ const lazy = e.target && e.target.closest('[data-component="lazy-belongs-to"]')
81
+ if (lazy !== lazySelect) {
82
+ hidePopout()
83
+ }
84
+ })
85
+
86
+ select.addEventListener('change', (e) => {
87
+ if (!e.currentTarget.value) {
88
+ return
89
+ }
90
+
91
+ target.value = e.currentTarget.value
92
+ button.textContent = e.currentTarget.selectedOptions[0].textContent
93
+ hidePopout()
94
+ })
95
+
96
+ button.removeAttribute('disabled')
97
+ })
98
+ }
99
+
100
+ if (Turbolinks && Turbolinks.supported) {
101
+ document.addEventListener("turbolinks:load", function () {
102
+ bindLazyBelongsTos()
103
+ })
104
+ } else {
105
+ document.addEventListener('DOMContentLoaded', bindLazyBelongsTos)
106
+ }
@@ -0,0 +1,4 @@
1
+ /*
2
+ *= require_self
3
+ *= require_tree .
4
+ */
@@ -0,0 +1,63 @@
1
+ .field-unit--lazy-belongs-to .popout {
2
+ display: none;
3
+ background-color: white;
4
+ box-shadow: rgba(13, 22, 38, 0.1) 0px 0px 0px 1px, rgba(13, 22, 38, 0.1) 0px 4px 11px;
5
+ margin-top: 8px;
6
+ position: absolute;
7
+ z-index: 2;
8
+ border-radius: 4px;
9
+ }
10
+
11
+ .field-unit--lazy-belongs-to .popout.active {
12
+ display: block;
13
+ }
14
+
15
+ .field-unit--lazy-belongs-to .search {
16
+ height: 100%;
17
+ width: auto;
18
+ line-height: 38px;
19
+ display: block;
20
+ padding: 8px;
21
+ position: relative;
22
+ box-sizing: border-box;
23
+ min-width: 240px;
24
+ border-color: rgb(204, 204, 204);
25
+ border-radius: 4px;
26
+ border-style: solid;
27
+ border-width: 1px;
28
+ transition: all 100ms ease 0s;
29
+ margin: 8px;
30
+ }
31
+
32
+ .field-unit--lazy-belongs-to .icon {
33
+ display: none;
34
+ position: absolute;
35
+ right: 15px;
36
+ top: 15px;
37
+ color: rgb(204, 204, 204);
38
+ height: 18px;
39
+ width: 18px;
40
+ pointer-events: none;
41
+ }
42
+
43
+ .field-unit--lazy-belongs-to .search:placeholder-shown + .field-unit--lazy-belongs-to .icon {
44
+ display: block;
45
+ }
46
+
47
+ .field-unit--lazy-belongs-to .results {
48
+ display: block;
49
+ box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 0px inset;
50
+ }
51
+
52
+ .field-unit--lazy-belongs-to .results select {
53
+ width: 100%;
54
+ border: 0;
55
+
56
+ -webkit-appearance: none;
57
+ -moz-appearance: none;
58
+ appearance: none;
59
+ }
60
+
61
+ .field-unit--lazy-belongs-to .results option {
62
+ padding: 8px;
63
+ }
@@ -0,0 +1,50 @@
1
+ <%#
2
+ # LazyBelongsTo Form Partial
3
+
4
+ This partial renders an input element for belongs_to relationships.
5
+ Instead of the default, this only loads the results lazily
6
+
7
+ ## Local variables:
8
+
9
+ - `f`:
10
+ A Rails form generator, used to help create the appropriate input fields.
11
+ - `field`:
12
+ An instance of [Administrate::Field::LazyBelongsTo][1].
13
+ A wrapper around the [Administrate::Field::BelongsTo][2] field
14
+
15
+ [1]: http://www.rubydoc.info/gems/administrate-field-lazy_belongs_to/Administrate/Field/LazyBelongsTo
16
+ [2]: http://www.rubydoc.info/gems/administrate/Administrate/Field/BelongsTo
17
+ %>
18
+
19
+ <div class="field-unit__label">
20
+ <%= f.label field.permitted_attribute %>
21
+ </div>
22
+ <div class="field-unit__field"
23
+ data-component="lazy-belongs-to"
24
+ data-lazy-belongs-to="<%=
25
+ {
26
+ url: field.templated_action,
27
+ value: field.value_attribute,
28
+ label: field.label_attribute
29
+ }.to_json
30
+ %>"
31
+ >
32
+ <%= f.hidden_field(field.permitted_attribute) %>
33
+
34
+ <button type="button" disabled="disabled"><%= field.current_value %></button>
35
+
36
+ <div class="popout" data-target="popout">
37
+ <input type="search" class="search" placeholder="Search...">
38
+ <i class="icon">
39
+ <svg width="18" height="18" viewBox="0 0 24 24" focusable="false" role="presentation">
40
+ <path d="M16.436 15.085l3.94 4.01a1 1 0 0 1-1.425 1.402l-3.938-4.006a7.5 7.5 0 1 1 1.423-1.406zM10.5 16a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11z" fill="currentColor" fill-rule="evenodd"></path>
41
+ </svg>
42
+ </i>
43
+ <output class="results" data-target="output">
44
+ <select data-max-size="<%= field.size %>">
45
+ <option></option>
46
+ </select>
47
+ </output>
48
+ </div>
49
+ </div>
50
+
@@ -0,0 +1,28 @@
1
+ <%#
2
+ # LazyBelongsTo Index Partial
3
+
4
+ This partial renders a belongs_to relationship,
5
+ to be displayed on a resource's index page.
6
+
7
+ By default, the relationship is rendered as a link to the associated object.
8
+
9
+ ## Local variables:
10
+
11
+ - `field`:
12
+ An instance of [Administrate::Field::LazyBelongsTo][1].
13
+ A wrapper around the [Administrate::Field::BelongsTo][2] field
14
+
15
+ [1]: http://www.rubydoc.info/gems/administrate-field-lazy_belongs_to/Administrate/Field/LazyBelongsTo
16
+ [2]: http://www.rubydoc.info/gems/administrate/Administrate/Field/BelongsTo
17
+ %>
18
+
19
+ <% if field.data %>
20
+ <% if valid_action?(:show, field.associated_class) %>
21
+ <%= link_to(
22
+ field.display_associated_resource,
23
+ [namespace, field.data],
24
+ ) %>
25
+ <% else %>
26
+ <%= field.display_associated_resource %>
27
+ <% end %>
28
+ <% end %>
@@ -0,0 +1,28 @@
1
+ <%#
2
+ # LazyBelongsTo Show Partial
3
+
4
+ This partial renders a belongs_to relationship,
5
+ to be displayed on a resource's show page.
6
+
7
+ By default, the relationship is rendered as a link to the associated object.
8
+
9
+ ## Local variables:
10
+
11
+ - `field`:
12
+ An instance of [Administrate::Field::LazyBelongsTo][1].
13
+ A wrapper around the [Administrate::Field::BelongsTo][2] field
14
+
15
+ [1]: http://www.rubydoc.info/gems/administrate-field-lazy_belongs_to/Administrate/Field/LazyBelongsTo
16
+ [2]: http://www.rubydoc.info/gems/administrate/Administrate/Field/BelongsTo
17
+ %>
18
+
19
+ <% if field.data %>
20
+ <% if valid_action?(:show, field.associated_class) %>
21
+ <%= link_to(
22
+ field.display_associated_resource,
23
+ [namespace, field.data],
24
+ ) %>
25
+ <% else %>
26
+ <%= field.display_associated_resource %>
27
+ <% end %>
28
+ <% end %>
data/example.png ADDED
Binary file
@@ -0,0 +1,7 @@
1
+ module Administrate
2
+ module Field
3
+ module LazyBelongsToVersion
4
+ VERSION = '0.1.0'.freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,48 @@
1
+ require 'administrate/field/belongs_to'
2
+ require 'administrate/field/lazy_belongs_to/version'
3
+ require 'rails/engine'
4
+
5
+ module Administrate
6
+ module Field
7
+ class LazyBelongsTo < Administrate::Field::BelongsTo
8
+ include LazyBelongsToVersion
9
+
10
+ class Engine < ::Rails::Engine
11
+ Administrate::Engine.add_javascript 'administrate-field-lazy_belongs_to/application'
12
+ Administrate::Engine.add_stylesheet 'administrate-field-lazy_belongs_to/application'
13
+
14
+ isolate_namespace Administrate
15
+ end
16
+
17
+ def to_s
18
+ data
19
+ end
20
+
21
+ def display_placeholder
22
+ options.fetch(:placeholder) do
23
+ format('Select a %<association>s', association: associated_class.name)
24
+ end
25
+ end
26
+
27
+ def current_value
28
+ data ? display_associated_resource : display_placeholder
29
+ end
30
+
31
+ def templated_action
32
+ options.fetch(:action).call(self, q: '{q}')
33
+ end
34
+
35
+ def value_attribute
36
+ options.fetch(:value_attribute) { 'id' }
37
+ end
38
+
39
+ def label_attribute
40
+ options.fetch(:label_attribute) { 'name' }
41
+ end
42
+
43
+ def url_helpers
44
+ Rails.application.routes.url_helpers
45
+ end
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: administrate-field-lazy_belongs_to
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Derk-Jan Karrenbeld
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: administrate
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.11.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '1'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 0.11.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '5.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '5.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rails
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ description:
90
+ email:
91
+ - derk-jan@xpbytes.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitattributes"
97
+ - ".gitignore"
98
+ - ".idea/.rakeTasks"
99
+ - ".idea/administrate-field-lazy_belongs_to.iml"
100
+ - ".idea/inspectionProfiles/Project_Default.xml"
101
+ - ".idea/misc.xml"
102
+ - ".idea/modules.xml"
103
+ - ".idea/vcs.xml"
104
+ - ".travis.yml"
105
+ - CHANGELOG.md
106
+ - Gemfile
107
+ - LICENSE
108
+ - README.md
109
+ - Rakefile
110
+ - administrate-field-lazy_belongs_to.gemspec
111
+ - app/assets/javascripts/administrate-field-lazy_belongs_to/application.js
112
+ - app/assets/javascripts/administrate-field-lazy_belongs_to/components/lazy_belongs_to.js
113
+ - app/assets/stylesheets/administate-field-lazy_belongs_to/application.css
114
+ - app/assets/stylesheets/administate-field-lazy_belongs_to/components/lazy_belongs_to.css
115
+ - app/views/fields/lazy_belongs_to/_form.html.erb
116
+ - app/views/fields/lazy_belongs_to/_index.html.erb
117
+ - app/views/fields/lazy_belongs_to/_show.html.erb
118
+ - example.png
119
+ - lib/administrate/field/lazy_belongs_to.rb
120
+ - lib/administrate/field/lazy_belongs_to/version.rb
121
+ homepage: https://github.com/XPBytes/administrate-field-lazy_belongs_to
122
+ licenses:
123
+ - MIT
124
+ metadata:
125
+ homepage_uri: https://github.com/XPBytes/administrate-field-lazy_belongs_to
126
+ source_code_uri: https://github.com/XPBytes/administrate-field-lazy_belongs_to
127
+ changelog_uri: https://github.com/XPBytes/administrate-field-lazy_belongs_to/CHANGELOG.md
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.7.6
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: A belongs to-like field that lazily loads candidates from a custom endpoint.
148
+ test_files: []