administrate-field-nested_has_many 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b2336e14b58c19c34cd27e3d18f6a64da56f97e0
4
+ data.tar.gz: 6638b8ec69dd50bb7bb4c0d0fb1987f314d4d790
5
+ SHA512:
6
+ metadata.gz: d100590240ed07f092bf2a7d0345fb14a4cf22be1474007e48dd23e680567dc9cd32c72e1d06cfd0cfe4cc23f6f27b5b5813af55c44898133e2412c6fbbff4fe
7
+ data.tar.gz: 39f13eef2ef5f7017981e7d4bf9dbd0c0b4bd3296cb6d3146e23cc0cdc1bf04538c317c66708b63c8575c61b4a30aa0927f478e77afe921acf2344c17bfe174a
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Administrate::Field::NestedHasMany
2
+
3
+ A plugin for nested has_many forms in [Administrate].
4
+
5
+ ## Usage
6
+
7
+ Add to your `Gemfile`:
8
+
9
+ ```ruby
10
+ gem "administrate-field-nested_has_many"
11
+ ```
12
+
13
+ Run:
14
+
15
+ ```bash
16
+ $bundle install
17
+ ```
18
+
19
+ Add to your `FooDashboard`:
20
+ ```ruby
21
+ ATTRIBUTE_TYPES = [
22
+ bars: Field::NestedHasMany.with_options(skip: :foo),
23
+ ]
24
+ ```
25
+
26
+ The `skip` option takes a single symbol or list of symbols.
27
+ It will prevent the nested form from displaying the fields for those attributes.
28
+
29
+ If a `Customer` `has_many :orders`,
30
+ and you want to render `orders` as a nested form on the customer edit page,
31
+ then it is generally necessary to add `skip: :customer` to the options
32
+ for the `NestedHasMany` field.
33
+ Otherwise, Administrate will try to render a field
34
+ for the order's `:customer` attribute,
35
+ which breaks the nested form logic.
36
+
37
+ [Administrate]: https://github.com/thoughtbot/administrate
@@ -0,0 +1,22 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ require "administrate/field/nested_has_many"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "administrate-field-nested_has_many"
7
+ gem.version = Administrate::Field::NestedHasMany::VERSION
8
+ gem.authors = ["Grayson Wright"]
9
+ gem.email = ["wright.grayson@gmail.com"]
10
+ gem.homepage = "https://github.com/graysonwright/administrate-field-nested_has_many"
11
+ gem.summary = "Plugin for nested has_many forms in Administrate"
12
+ gem.description = gem.summary
13
+ gem.license = "MIT"
14
+
15
+ gem.require_paths = ["lib"]
16
+ gem.files = `git ls-files`.split("\n")
17
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+
19
+ gem.add_dependency "administrate", "~> 0.2.0"
20
+ gem.add_dependency "cocoon", "~> 1.2"
21
+ gem.add_dependency "rails", "~> 4.2"
22
+ end
@@ -0,0 +1,11 @@
1
+ <div class="nested-fields" style="width:100%">
2
+
3
+ <% field.nested_fields.each do |attribute| -%>
4
+ <div class="field-unit field-unit--<%= attribute.html_class %>">
5
+ <%= render_field attribute, f: f %>
6
+ </div>
7
+ <% end -%>
8
+
9
+ <%# TODO I18n %>
10
+ <%= link_to_remove_association "Remove #{field.associated_class_name.titleize}", f %>
11
+ </div>
@@ -0,0 +1,23 @@
1
+ <div class="nested-fields" style="width: 40%; margin-left: 20%;">
2
+
3
+ <%= f.fields_for field.association_name do |nested_form| %>
4
+ <%= render(
5
+ partial: "fields/nested_has_many/fields",
6
+ locals: {
7
+ f: nested_form,
8
+ field: field,
9
+ },
10
+ ) %>
11
+ <% end %>
12
+
13
+ <div>
14
+ <%= link_to_add_association(
15
+ # TODO I18n
16
+ "Add #{field.associated_class_name.titleize}",
17
+ f,
18
+ field.association_name,
19
+ partial: "fields/nested_has_many/fields",
20
+ render_options: { locals: { field: field } },
21
+ ) %>
22
+ </div>
23
+ </div>
@@ -0,0 +1,19 @@
1
+ <%#
2
+ # HasMany Index Partial
3
+
4
+ This partial renders a has_many relationship,
5
+ to be displayed on a resource's index page.
6
+
7
+ By default, the relationship is rendered
8
+ as a count of how many objects are associated through the relationship.
9
+
10
+ ## Local variables:
11
+
12
+ - `field`:
13
+ An instance of [Administrate::Field::HasMany][1].
14
+ A wrapper around the has_many relationship pulled from the database.
15
+
16
+ [1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/HasMany
17
+ %>
18
+
19
+ <%= pluralize(field.data.size, field.attribute.to_s.humanize.downcase) %>
@@ -0,0 +1,40 @@
1
+ <%#
2
+ # HasMany Show Partial
3
+
4
+ This partial renders a has_many relationship,
5
+ to be displayed on a resource's show page.
6
+
7
+ By default, the relationship is rendered
8
+ as a table of the first few associated resources.
9
+ The columns of the table are taken
10
+ from the associated resource class's dashboard.
11
+
12
+ ## Local variables:
13
+
14
+ - `field`:
15
+ An instance of [Administrate::Field::HasMany][1].
16
+ Contains methods to help display a table of associated resources.
17
+
18
+ [1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/HasMany
19
+ %>
20
+
21
+ <% if field.resources.any? %>
22
+ <%= render(
23
+ "collection",
24
+ collection_presenter: field.associated_collection,
25
+ resources: field.resources
26
+ ) %>
27
+
28
+ <% if field.more_than_limit? %>
29
+ <span>
30
+ <%= t(
31
+ 'administrate.fields.has_many.more',
32
+ count: field.limit,
33
+ total_count: field.data.count,
34
+ ) %>
35
+ </span>
36
+ <% end %>
37
+
38
+ <% else %>
39
+ <%= t("administrate.fields.has_many.none") %>
40
+ <% end %>
@@ -0,0 +1,60 @@
1
+ require "administrate/field/has_many"
2
+ require "administrate/page/form"
3
+ require "rails"
4
+
5
+ module Administrate
6
+ module Field
7
+ class NestedHasMany < Administrate::Field::HasMany
8
+ VERSION = "0.0.1"
9
+
10
+ class Engine < ::Rails::Engine
11
+ end
12
+
13
+ DEFAULT_ATTRIBUTES = [:id, :_destroy].freeze
14
+
15
+ def nested_fields
16
+ associated_form.attributes.reject do |nested_field|
17
+ skipped_fields.include?(nested_field.attribute)
18
+ end
19
+ end
20
+
21
+ def to_s
22
+ data
23
+ end
24
+
25
+ def self.dashboard_for_resource(resource)
26
+ "#{resource.to_s.classify}Dashboard".constantize
27
+ end
28
+
29
+ def self.associated_attributes(associated_resource)
30
+ DEFAULT_ATTRIBUTES +
31
+ dashboard_for_resource(associated_resource).new.permitted_attributes
32
+ end
33
+
34
+ def self.permitted_attribute(associated_resource)
35
+ {
36
+ "#{associated_resource}_attributes".to_sym =>
37
+ associated_attributes(associated_resource),
38
+ }
39
+ end
40
+
41
+ def associated_class_name
42
+ options.fetch(:class_name, attribute.to_s.singularize.camelcase)
43
+ end
44
+
45
+ def association_name
46
+ associated_class_name.underscore.pluralize
47
+ end
48
+
49
+ def associated_form
50
+ Administrate::Page::Form.new(associated_dashboard, association_name)
51
+ end
52
+
53
+ private
54
+
55
+ def skipped_fields
56
+ Array(options[:skip])
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,14 @@
1
+ require "administrate/field/nested_has_many"
2
+
3
+ describe Administrate::Field::NestedHasMany do
4
+ describe "#to_partial_path" do
5
+ it "returns a partial based on the page being rendered" do
6
+ page = :show
7
+ field = Administrate::Field::NestedHasMany.new(:relation, "/a.jpg", page)
8
+
9
+ path = field.to_partial_path
10
+
11
+ expect(path).to eq("/fields/nested_has_many/#{page}")
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: administrate-field-nested_has_many
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Grayson Wright
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-21 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.2.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.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: cocoon
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ description: Plugin for nested has_many forms in Administrate
56
+ email:
57
+ - wright.grayson@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - administrate-field-nested_has_many.gemspec
64
+ - app/assets/javascripts/administrate-field-nested_has_many/application.js
65
+ - app/views/fields/nested_has_many/_fields.html.erb
66
+ - app/views/fields/nested_has_many/_form.html.erb
67
+ - app/views/fields/nested_has_many/_index.html.erb
68
+ - app/views/fields/nested_has_many/_show.html.erb
69
+ - lib/administrate/field/nested_has_many.rb
70
+ - spec/lib/administrate/field/nested_has_many_spec.rb
71
+ homepage: https://github.com/graysonwright/administrate-field-nested_has_many
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.5.1
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Plugin for nested has_many forms in Administrate
95
+ test_files:
96
+ - spec/lib/administrate/field/nested_has_many_spec.rb