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

Create a dashbard generator #20

Merged
merged 1 commit into from Jun 2, 2015
Merged

Create a dashbard generator #20

merged 1 commit into from Jun 2, 2015

Conversation

c-lliope
Copy link
Contributor

The generator pulls column names and types from the database,
using the methods #column_names and column_types.

  • Fill in missing mappings to BaseDashboard#field_registry.

References:
http://edgeguides.rubyonrails.org/generators.html
https://github.com/ryanb/nifty-generators

When the user runs rails generate dashboard order,
they get a file app/dashboards/order_dashboard.rb with the contents:

require "base_dashboard"

class OrderDashboard < BaseDashboard

  # This method returns a hash
  # that describes the type of each of the model's fields.
  #
  # Each different type represents an Administrate::Field object,
  # which determines how the attribute is displayed
  # on pages throughout the dashboard.
  def attribute_types
    {
      id: :integer,
      customer_id: :integer,
      address_line_one: :string,
      address_line_two: :string,
      address_city: :string,
      address_state: :string,
      address_zip: :string,
      created_at: :datetime,
      updated_at: :datetime,
      customer: :belongs_to,
      line_items: :has_many,
    }
  end

  # This method returns an array of attributes
  # that will be displayed on the model's index page.
  def index_page_attributes
    attributes
  end

  # This method returns an array of attributes
  # that will be displayed on the model's show page
  def show_page_attributes
    attributes
  end

  # This method returns an array of attributes
  # that will be displayed on the model's form pages (`new` and `edit`)
  def form_attributes
    attributes - read_only_attributes
  end

  private

  def attributes
    [
      :id,
      :customer_id,
      :address_line_one,
      :address_line_two,
      :address_city,
      :address_state,
      :address_zip,
      :created_at,
      :updated_at,
      :customer,
      :line_items,
    ]
  end

  def read_only_attributes
    [
      :id,
      :created_at,
      :updated_at,
    ]
  end
end

https://trello.com/c/My9ldlzZ

@@ -0,0 +1,17 @@
class DashboardGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)

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.

@c-lliope
Copy link
Contributor Author

  • make sure it works for has_many relationships
  • make sure it works for belongs_to relationships
  • add light comments to describe the API?

:lifetime_value,
:created_at,
:updated_at,
]
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you clarify why this file was updated within the context of this commit? Wondering if it is related/within the context of the commit's purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it's not super related. I updated the structure a little to reflect the generated dashboard.

@c-lliope
Copy link
Contributor Author

Any thoughts on how to test this?

private

def attributes
klass.column_names +
Copy link

Choose a reason for hiding this comment

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

attribute_names

@derekprior
Copy link

This looks correct to me from a generator standpoint. Is the reflection stuff public API?

Ammeter can help you test the file creation aspect of this, verify the generated file is valid ruby, etc. Faking a model out and all of that reflection seems like it would be hard. I'd probably try to have a real model available for the test.

@sgrif
Copy link

sgrif commented May 22, 2015

Is the reflection stuff public API?

It's probably the most ambiguous part of Active Record as to whether it's public or not. Generally speaking, no.

:belongs_to
else
throw "Unknown association type: #{reflection.inspect}\n" +
"Please open an issue on the Administrate repo."

Choose a reason for hiding this comment

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

Use 2 (not 0) spaces for indenting an expression spanning multiple lines.

@c-lliope
Copy link
Contributor Author

Related deprecation issues on ActiveAdmin: activeadmin/activeadmin#3930

The generator pulls column names and types from the database,
using the methods `.type_for_attribute` and `.reflections`.

- Fill in missing mappings to `BaseDashboard#field_registry`.

References:
http://edgeguides.rubyonrails.org/generators.html
https://github.com/ryanb/nifty-generators

https://trello.com/c/My9ldlzZ
@c-lliope
Copy link
Contributor Author

c-lliope commented Jun 2, 2015

Alright, I'm willing to merge this for a few reasons:

  • We're going for an MVP here. The only requirement is that it works with the latest version of Rails (nothing more, nothing less).
  • Without a clearer idea of how Rails 5 will be structured, it'll be pretty rough trying to find a forward-compatible solution.
  • This is generator code. If people upgrade to Rails 5, their already-generated code will continue to work.
  • If worst comes to worst, we can always create adapters for different major versions of Rails.

@sgrif I'm really interested in playing with the Attributes API, and making sure this is forwards-compatible before we launch 1.0. Thanks for the advice with this! I'm sure we'll have some more conversations along similar lines before too long 😄

@c-lliope c-lliope merged commit e9955fc into master Jun 2, 2015
@c-lliope c-lliope deleted the gw-generator branch June 2, 2015 05:58
@c-lliope c-lliope temporarily deployed to administrate-prototype June 2, 2015 05:59 Inactive
jordan-brough referenced this pull request in jordan-brough/administrate Jan 20, 2022
As of Ruby 3.0 webrick is no longer part of the Ruby standard library:
https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/
drnic referenced this pull request in drnic/administrate Oct 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants