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

Add support for selectable strings on edit pages #422

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions app/views/fields/select/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<%#
# Select Index Partial

This partial renders a selectable text attribute,
to be displayed on a resource's edit form page.

## Local variables:

- `f`:
A Rails form generator, used to help create the appropriate input fields.
- `field`:
An instance of [Administrate::Field::Select][1].
A wrapper around the attribute pulled from the database.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Select
%>

<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.select(
field.attribute,
options_from_collection_for_select(
field.selectable_options,
:to_s,
:to_s,
field.data.presence,
)
) %>
</div>
16 changes: 16 additions & 0 deletions app/views/fields/select/_index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%#
# Select Index Partial

This partial renders a selectable text attribute,
to be displayed on a resource's index page.

## Local variables:

- `field`:
An instance of [Administrate::Field::Select][1].
A wrapper around the attribute pulled from the database.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Select
%>

<%= field.data %>
16 changes: 16 additions & 0 deletions app/views/fields/select/_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%#
# Select Show Partial

This partial renders a selectable text attribute,
to be displayed on a resource's show page.

## Local variables:

- `field`:
An instance of [Administrate::Field::Select][1].
A wrapper around the attribute pulled from the database.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Select
%>

<%= field.data %>
1 change: 1 addition & 0 deletions lib/administrate/base_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "administrate/field/image"
require "administrate/field/number"
require "administrate/field/polymorphic"
require "administrate/field/select"
require "administrate/field/string"
require "administrate/field/text"

Expand Down
21 changes: 21 additions & 0 deletions lib/administrate/field/select.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative "base"

module Administrate
module Field
class Select < Field::Base
def self.searchable?
true
end

def selectable_options
collection
end

private

def collection
@collection ||= options.fetch(:collection, [])
end
end
end
end
8 changes: 7 additions & 1 deletion spec/example_app/app/dashboards/customer_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ class CustomerDashboard < Administrate::BaseDashboard
name: Field::String,
orders: Field::HasMany,
updated_at: Field::DateTime,
kind: Field::Select.with_options(collection: Customer::KINDS),
}

COLLECTION_ATTRIBUTES = ATTRIBUTE_TYPES.keys
SHOW_PAGE_ATTRIBUTES = ATTRIBUTE_TYPES.keys - [:name]
FORM_ATTRIBUTES = [:name, :email, :email_subscriber]
FORM_ATTRIBUTES = [
:name,
:email,
:email_subscriber,
:kind,
].freeze

def display_resource(customer)
customer.name
Expand Down
5 changes: 5 additions & 0 deletions spec/example_app/app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ class Customer < ActiveRecord::Base
validates :name, presence: true
validates :email, presence: true

KINDS = [

Choose a reason for hiding this comment

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

Freeze mutable objects assigned to constants.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This array is frozen -- Hound isn't catching that it's a couple lines down.

:standard,
:vip,
].freeze

def lifetime_value
orders.map(&:total_price).reduce(0, :+)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddKindToCustomer < ActiveRecord::Migration
def change
add_column :customers, :kind, :string, null: false, default: "standard"
end
end
3 changes: 2 additions & 1 deletion spec/example_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150916011117) do
ActiveRecord::Schema.define(version: 20160119024340) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -22,6 +22,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "email_subscriber"
t.string "kind", default: "standard", null: false
end

create_table "delayed_jobs", force: :cascade do |t|
Expand Down
11 changes: 11 additions & 0 deletions spec/features/edit_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,15 @@

expect(page).to have_content("true")
end

it "displays selectable strings as dropdowns", :js do
customer = create(:customer, kind: :standard)

visit edit_admin_customer_path(customer)
select "vip", from: "Kind"
click_on "Update Customer"

expect(page).to have_content("KIND")
expect(page).to have_content("vip")
end
end