diff --git a/docs/customizing_attribute_partials.md b/docs/customizing_attribute_partials.md index 71147486d62bd4c875638746d0fd21595dbc44fc..007c4965f673206875e51e4a7447e14550aa85f0 100644 --- a/docs/customizing_attribute_partials.md +++ b/docs/customizing_attribute_partials.md @@ -1,7 +1,20 @@ # Customizing attribute partials Occasionally you might want to change how specific types of attributes appear -across all dashboards. +across all dashboards. You can customize the following built in field types: + +- `belongs_to` +- `boolean` +- `date_time` +- `email` +- `has_many` +- `has_one` +- `number` +- `polymporphic` +- `select` +- `string` +- `text` + For example, you might want all `Number` values to round to three decimal points. To get started, run the appropriate rails generator: @@ -16,6 +29,12 @@ This will generate three files: - `app/view/fields/number/_index.html.erb` - `app/view/fields/number/_show.html.erb` +You can generate the partials for all field types by passing `all` to the generator. + +```bash +rails generate administrate:views:field all +``` + The generated templates will have documentation describing which variables are in scope. The rendering part of the partial will look like: diff --git a/lib/generators/administrate/views/field_generator.rb b/lib/generators/administrate/views/field_generator.rb index 61110678bb5f2a93e6a8575880eddedd68548488..c883338d3ad6d1aa9e7da923adc4121c37420f53 100644 --- a/lib/generators/administrate/views/field_generator.rb +++ b/lib/generators/administrate/views/field_generator.rb @@ -14,15 +14,29 @@ module Administrate source_root template_source_path def copy_partials - copy_field_partial(:index) - copy_field_partial(:show) - copy_field_partial(:form) + resource_path = args.first.try(:underscore) + + if resource_path == "all" + entries = Dir.entries(self.class.template_source_path) + field_types = entries.reject { |name| name[0] == "." } + + field_types.each do |field_type| + copy_field_partials(field_type) + end + else + copy_field_partials(resource_path) + end end private - def copy_field_partial(partial_name) - resource_path = args.first.try(:underscore) + def copy_field_partials(resource_path) + copy_field_partial(resource_path, :index) + copy_field_partial(resource_path, :show) + copy_field_partial(resource_path, :form) + end + + def copy_field_partial(resource_path, partial_name) template_file = "#{resource_path}/_#{partial_name}.html.erb" copy_file( diff --git a/spec/generators/views/field_generator_spec.rb b/spec/generators/views/field_generator_spec.rb index 54c4467430e4d8d4350558052cf2d65594a58c91..1feb4e782c4845d07ae68de3e32b2d0cfcf55f7e 100644 --- a/spec/generators/views/field_generator_spec.rb +++ b/spec/generators/views/field_generator_spec.rb @@ -1,4 +1,4 @@ -require "spec_helper" +require "rails_helper" require "generators/administrate/views/field_generator" require "support/generator_spec_helpers" @@ -32,6 +32,51 @@ describe Administrate::Generators::Views::FieldGenerator, :generator do expect(contents).to eq(expected_contents) end end + + describe "administrate:views:field all" do + let(:field_types) do + Dir.entries("app/views/fields").reject { |name| name[0] == "." } + end + + it "copies the `_show` partial for each field type" do + run_generator ["all"] + + field_types.each do |field_type| + expected_contents = contents_for_field_template(field_type, :show) + contents = File.read( + file("app/views/fields/#{field_type}/_show.html.erb"), + ) + + expect(contents).to eq(expected_contents) + end + end + + it "copies the `_form` partial for each field type" do + run_generator ["all"] + + field_types.each do |field_type| + expected_contents = contents_for_field_template(field_type, :form) + contents = File.read( + file("app/views/fields/#{field_type}/_form.html.erb"), + ) + + expect(contents).to eq(expected_contents) + end + end + + it "copies the `_index` partial for each field type" do + run_generator ["all"] + + field_types.each do |field_type| + expected_contents = contents_for_field_template(field_type, :index) + contents = File.read( + file("app/views/fields/#{field_type}/_index.html.erb"), + ) + + expect(contents).to eq(expected_contents) + end + end + end end def contents_for_field_template(field_name, partial_name)