Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A administrate
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 96
    • Issues 96
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 32
    • Merge requests 32
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • thoughtbot, inc.
  • administrate
  • Merge requests
  • !20

Create a dashbard generator

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge gw-generator into master 10 years ago
  • Overview 7
  • Commits 1
  • Pipelines 0
  • Changes 7

Created by: gracewashere

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

Compare
  • master (base)

and
  • latest version
    e9955fcd
    1 commit, 2 years ago

7 files
+ 148
- 0

    Preferences

    File browser
    Compare changes
app/das‎hboards‎
customer_d‎ashboard.rb‎ +4 -0
l‎ib‎
generators‎/dashboard‎
temp‎lates‎
dashboar‎d.rb.erb‎ +50 -0
US‎AGE‎ +9 -0
dashboard_g‎enerator.rb‎ +34 -0
base_das‎hboard.rb‎ +2 -0
spec/ge‎nerators‎
dashboard_gen‎erator_spec.rb‎ +48 -0
Gem‎file‎ +1 -0
app/dashboards/customer_dashboard.rb
+ 4
- 0
  • View file @ e9955fcd

  • Edit in single-file editor

  • Open in Web IDE


@@ -3,10 +3,12 @@ require "base_dashboard"
class CustomerDashboard < BaseDashboard
def attribute_types
{
created_at: :datetime,
email: :email,
lifetime_value: :string,
name: :string,
orders: :has_many,
updated_at: :datetime,
}
end
@@ -33,6 +35,8 @@ class CustomerDashboard < BaseDashboard
:email,
:lifetime_value,
:orders,
:created_at,
:updated_at,
]
end
end
lib/generators/dashboard/templates/dashboard.rb.erb 0 → 100644
+ 50
- 0
  • View file @ e9955fcd

  • Edit in single-file editor

  • Open in Web IDE

require "base_dashboard"
class <%= class_name %>Dashboard < 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
{<% attributes.each do |attr| %>
<%= attr %>: :<%= field_type(attr) %>,<% end %>
}
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
[<% attributes.each do |attribute| %>
:<%= attribute %>,<% end %>
]
end
def read_only_attributes
[
:id,
:created_at,
:updated_at,
]
end
end
lib/generators/dashboard/USAGE 0 → 100644
+ 9
- 0
  • View file @ e9955fcd

  • Edit in single-file editor

  • Open in Web IDE

Description:
Generates a Dashboard object for a model,
pulling the attributes from database columns.
Example:
rails generate dashboard FooBar
This will create:
app/dashboards/foo_bar_dashboard.rb
lib/generators/dashboard/dashboard_generator.rb 0 → 100644
+ 34
- 0
  • View file @ e9955fcd

  • Edit in single-file editor

  • Open in Web IDE

class DashboardGenerator < Rails::Generators::NamedBase
source_root File.expand_path("../templates", __FILE__)
def copy_dashboard_file
template "dashboard.rb.erb", "app/dashboards/#{file_name}_dashboard.rb"
end
private
def attributes
klass.attribute_names + klass.reflections.keys
end
def field_type(attribute)
klass.type_for_attribute(attribute).type ||
association_type(attribute)
end
def association_type(attribute)
reflection = klass.reflections[attribute.to_s]
if reflection.collection?
:has_many
elsif reflection.belongs_to?
:belongs_to
else
throw "Unknown association type: #{reflection.inspect}\n" +
"Please open an issue on the Administrate repo."
end
end
def klass
@klass ||= Object.const_get(class_name)
end
end
lib/base_dashboard.rb
+ 2
- 0
  • View file @ e9955fcd

  • Edit in single-file editor

  • Open in Web IDE


@@ -20,9 +20,11 @@ class BaseDashboard
def field_registry
{
belongs_to: Field::BelongsTo,
datetime: Field::String,
email: Field::Email,
has_many: Field::HasMany,
image: Field::Image,
integer: Field::String,
string: Field::String,
}
end
spec/generators/dashboard_generator_spec.rb 0 → 100644
+ 48
- 0
  • View file @ e9955fcd

  • Edit in single-file editor

  • Open in Web IDE

require "rails_helper"
require "generators/dashboard/dashboard_generator"
describe DashboardGenerator, :generator do
it "has valid syntax" do
dashboard = file("app/dashboards/customer_dashboard.rb")
run_generator ["customer"]
expect(dashboard).to exist
expect(dashboard).to have_correct_syntax
end
it "includes standard model attributes" do
dashboard = file("app/dashboards/customer_dashboard.rb")
run_generator ["customer"]
expect(dashboard).to contain("id: :integer,")
expect(dashboard).to contain("created_at: :datetime,")
expect(dashboard).to contain("updated_at: :datetime,")
end
it "includes user-defined database columns" do
dashboard = file("app/dashboards/customer_dashboard.rb")
run_generator ["customer"]
expect(dashboard).to contain("name: :string,")
expect(dashboard).to contain("email: :string,")
end
it "includes has_many relationships" do
dashboard = file("app/dashboards/customer_dashboard.rb")
run_generator ["customer"]
expect(dashboard).to contain("orders: :has_many")
end
it "includes belongs_to relationships" do
dashboard = file("app/dashboards/order_dashboard.rb")
run_generator ["order"]
expect(dashboard).to contain("customer: :belongs_to")
end
end
Gemfile
+ 1
- 0
  • View file @ e9955fcd

  • Edit in single-file editor

  • Open in Web IDE


@@ -43,6 +43,7 @@ group :development, :test do
end
group :test do
gem "ammeter"
gem "capybara-webkit", ">= 1.2.0"
gem "database_cleaner"
gem "formulaic"
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Milestone
No milestone
None
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
0
0 participants
Reference: firstcontributions/first-contributions!12019
Source branch: gw-generator

Menu

Explore Projects Groups Snippets