Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field generators #992

Merged
merged 4 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion docs/customizing_attribute_partials.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand Down
24 changes: 19 additions & 5 deletions lib/generators/administrate/views/field_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,36 @@ def self.template_source_path
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'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

field_types = Dir.entries(self.class.template_source_path).reject{ |name| name[0] == "."}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space missing to the left of {.
Line is too long. [101/80]
Space missing inside }.


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(
template_file,
"app/views/fields/#{template_file}",
)
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at class body end.

end
end
end
Expand Down
44 changes: 43 additions & 1 deletion spec/generators/views/field_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require "spec_helper"
require "rails_helper"
require "generators/administrate/views/field_generator"
require "support/generator_spec_helpers"

describe Administrate::Generators::Views::FieldGenerator, :generator do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at block body beginning.

context "for an existing field type" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at block body beginning.

describe "administrate:views:field field_name" do
it "copies the `_show` partial into the app/views/fields directory" do
expected_contents = contents_for_field_template(:string, :show)
Expand Down Expand Up @@ -32,6 +34,46 @@
expect(contents).to eq(expected_contents)
end
end

describe "administrate:views:field all" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at block body beginning.

let(:field_types) { Dir.entries("app/views/fields").reject{ |name| name[0] == "."} }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space missing to the left of {.
Line is too long. [90/80]
Space missing inside }.


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"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [85/80]


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"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [85/80]


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"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [86/80]


expect(contents).to eq(expected_contents)
end
end

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at block body end.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at block body end.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at block body end.

end

def contents_for_field_template(field_name, partial_name)
Expand Down