Skip to content

Commit

Permalink
Improve field generators (#992)
Browse files Browse the repository at this point in the history
* Added a list of built in field types to docs
* Added 'all' option to views field generator
* Updated docs to include new 'all' param for view field generator
  • Loading branch information
ryanhertz authored and nickcharlton committed Oct 27, 2017
1 parent 3366bd1 commit e672316
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
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,15 +14,29 @@ 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"
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(
Expand Down
47 changes: 46 additions & 1 deletion spec/generators/views/field_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "spec_helper"
require "rails_helper"
require "generators/administrate/views/field_generator"
require "support/generator_spec_helpers"

Expand Down Expand Up @@ -32,6 +32,51 @@
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)
Expand Down

0 comments on commit e672316

Please sign in to comment.