administrate-field-ordered_has_many 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: 6e28e3833eb0a0c6d63fb18214c66cdb4050feeda8890807e62458c68aeab924
4
+ data.tar.gz: 0d69d468cee3995fb9c6f673db65b1b0555c76c1a329b532ba38355be25bf737
5
+ SHA512:
6
+ metadata.gz: 44b5c625e63bf37b4e9459d465b9f587adab939405b82dba7a31f8abad70a2ee77997953aa0537690ff2e87ace361fe44ed43016583281c45739476015dd0eca
7
+ data.tar.gz: 6200e452c1e529201252c87df6c88863fd5ed6d316e0f8df027903adb9cd69002e461eb7672ed082e557470e44bbd4d57b8b9fa57f1218c9d6dac1b658227882
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Administrate::Field::OrderedHasMany
2
+
3
+ This plugin allows you to manage ordered `has_many` relationships within [Administrate](https://github.com/thoughtbot/administrate). The relationships must be defined on the parent in the form of a SQL array field with the name `ordered_#{association_name}_ids`.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'administrate-field-ordered_has_many'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install administrate-field-ordered_has_many
20
+
21
+ ## Usage
22
+
23
+ In the dashboard for the parent resource, set the attribute type for your ordered `has_many` association to `Field::OrderedHasMany`. The edit UI will now show a set of select boxes for each association defined by the array value.
24
+
25
+ ## Development
26
+
27
+ 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).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/coderedcode/administrate-field-ordered_has_many](https://github.com/coderedcode/administrate-field-ordered_has_many).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "administrate/field/ordered_has_many/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "administrate-field-ordered_has_many"
7
+ spec.version = Administrate::Field::OrderedHasMany::VERSION
8
+ spec.authors = ["Kristof Redei"]
9
+ spec.email = ["kristof@coderedcode.com"]
10
+
11
+ spec.summary = "Plugin that allows managing ordered has_many associations within Administrate."
12
+ spec.homepage = "https://github.com/coderedcode/administrate-field-ordered_has_many"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
16
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "administrate"
23
+ spec.add_runtime_dependency "rails", ">= 5.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,51 @@
1
+ <div class="field-unit__label">
2
+ <%= f.label field.association_name %>
3
+ </div>
4
+
5
+ <div class="field-unit__field">
6
+
7
+ <% _group_id = "gid_#{SecureRandom.alphanumeric}" %>
8
+
9
+ <% field.values.each do |v| %>
10
+ <p>
11
+ <%= f.select(field.attribute, options_from_collection_for_select(
12
+ field.selectable_options, :id, :name, v),
13
+ {include_blank: true},
14
+ { name: "#{f.object_name}[#{field.attribute}][]",
15
+ class: _group_id})
16
+ %>
17
+ </p>
18
+ <% end %>
19
+
20
+ <p>
21
+ <%= f.select(field.attribute, options_from_collection_for_select(
22
+ field.selectable_options, :id, :name, nil),
23
+ { include_blank: true },
24
+ { name: "#{f.object_name}[#{field.attribute}][]",
25
+ class: _group_id}) %>
26
+ </p>
27
+ <%= button_tag(
28
+ '+',
29
+ onclick: 'var $l = $(".' + _group_id + '").last(); $l.clone(false).insertAfter($l).val(""); return false;',
30
+ )
31
+ %>
32
+
33
+ <script>
34
+ (function() {
35
+ var group_selector = '.<%= _group_id %>'
36
+
37
+ document.addEventListener("DOMContentLoaded", function(evt) {
38
+
39
+ var $f = $(group_selector).parents('form');
40
+
41
+ $f.on('submit', function() {
42
+ // Filter empty fileds before submit
43
+ $(group_selector).filter(function() {
44
+ return !this.value;
45
+ }).removeAttr('name');
46
+ });
47
+ });
48
+ })()
49
+ </script>
50
+
51
+ </div>
@@ -0,0 +1 @@
1
+ <%= field.to_s %>
@@ -0,0 +1 @@
1
+ <%= field.to_s %>
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "administrate/field/ordered_has_many"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,48 @@
1
+ require "administrate/field/base"
2
+ require "rails"
3
+
4
+ module Administrate
5
+ module Field
6
+ class OrderedHasMany < Administrate::Field::Base
7
+ def self.permitted_attribute(attr, _options = nil)
8
+ {attr => []}
9
+ end
10
+
11
+ def to_s
12
+ return "-" unless data
13
+ associated_class.find(data).map do |associated_instance|
14
+ associated_instance.try(:name) || associated_instance.to_s
15
+ end.join(", ")
16
+ end
17
+
18
+ def association_name
19
+ self.attribute.match(/ordered_(.+)_ids/)[1].humanize.pluralize
20
+ end
21
+
22
+ def associated_class
23
+ class_name = self.attribute.match(/ordered_(.+)_ids/)[1].camelcase
24
+ begin
25
+ class_name.constantize
26
+ rescue
27
+ raise "#{class_name} is not a valid association!"
28
+ end
29
+ end
30
+
31
+ def selectable_options
32
+ associated_class.all
33
+ end
34
+
35
+ def values
36
+ return [] unless data
37
+ if data.is_a?(Array)
38
+ data
39
+ else
40
+ raise "#{self.attribute} must be an array of #{associated_class} ID's, instead it was #{data.inspect}"
41
+ end
42
+ end
43
+
44
+ class Engine < ::Rails::Engine; end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,7 @@
1
+ module Administrate
2
+ module Field
3
+ module OrderedHasMany
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: administrate-field-ordered_has_many
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kristof Redei
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-09-03 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - kristof@coderedcode.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - administrate-field-ordered_has_many.gemspec
82
+ - app/views/fields/ordered_has_many/_form.html.erb
83
+ - app/views/fields/ordered_has_many/_index.html.erb
84
+ - app/views/fields/ordered_has_many/_show.html.erb
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/administrate/field/ordered_has_many.rb
88
+ - lib/administrate/field/ordered_has_many/version.rb
89
+ homepage: https://github.com/coderedcode/administrate-field-ordered_has_many
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.7.6
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Plugin that allows managing ordered has_many associations within Administrate.
113
+ test_files: []