From 2c4ed978e43e25f7b2c28124ce593b6b4609adb6 Mon Sep 17 00:00:00 2001 From: Jason Berlinsky Date: Mon, 18 Jan 2016 22:33:23 -0500 Subject: [PATCH] Add support for selectable strings on edit pages --- app/views/fields/select/_form.html.erb | 26 +++++++++++++++++++ app/views/fields/select/_index.html.erb | 16 ++++++++++++ app/views/fields/select/_show.html.erb | 16 ++++++++++++ lib/administrate/base_dashboard.rb | 1 + lib/administrate/fields/select.rb | 21 +++++++++++++++ .../app/dashboards/customer_dashboard.rb | 3 ++- spec/example_app/app/models/customer.rb | 5 ++++ .../20160119024340_add_kind_to_customer.rb | 5 ++++ spec/example_app/db/schema.rb | 3 ++- spec/features/edit_page_spec.rb | 22 ++++++++++++++++ 10 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 app/views/fields/select/_form.html.erb create mode 100644 app/views/fields/select/_index.html.erb create mode 100644 app/views/fields/select/_show.html.erb create mode 100644 lib/administrate/fields/select.rb create mode 100644 spec/example_app/db/migrate/20160119024340_add_kind_to_customer.rb diff --git a/app/views/fields/select/_form.html.erb b/app/views/fields/select/_form.html.erb new file mode 100644 index 0000000000..3accbc7237 --- /dev/null +++ b/app/views/fields/select/_form.html.erb @@ -0,0 +1,26 @@ +<%# +# 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 +%> + +
+ <%= f.label field.attribute %> +
+
+ <%= f.select( + field.attribute, + options_from_collection_for_select(field.selectable_options, :to_s, :to_s) + ) %> +
diff --git a/app/views/fields/select/_index.html.erb b/app/views/fields/select/_index.html.erb new file mode 100644 index 0000000000..4b6eeb28d3 --- /dev/null +++ b/app/views/fields/select/_index.html.erb @@ -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.presence %> diff --git a/app/views/fields/select/_show.html.erb b/app/views/fields/select/_show.html.erb new file mode 100644 index 0000000000..0569df2a61 --- /dev/null +++ b/app/views/fields/select/_show.html.erb @@ -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.presence %> diff --git a/lib/administrate/base_dashboard.rb b/lib/administrate/base_dashboard.rb index a542b698ac..293dd8e6c2 100644 --- a/lib/administrate/base_dashboard.rb +++ b/lib/administrate/base_dashboard.rb @@ -7,6 +7,7 @@ require "administrate/fields/image" require "administrate/fields/number" require "administrate/fields/polymorphic" +require "administrate/fields/select" require "administrate/fields/string" require "administrate/fields/text" diff --git a/lib/administrate/fields/select.rb b/lib/administrate/fields/select.rb new file mode 100644 index 0000000000..73b0ea570f --- /dev/null +++ b/lib/administrate/fields/select.rb @@ -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 diff --git a/spec/example_app/app/dashboards/customer_dashboard.rb b/spec/example_app/app/dashboards/customer_dashboard.rb index f62d2c01c9..c4c9a20c59 100644 --- a/spec/example_app/app/dashboards/customer_dashboard.rb +++ b/spec/example_app/app/dashboards/customer_dashboard.rb @@ -9,11 +9,12 @@ 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] def display_resource(customer) customer.name diff --git a/spec/example_app/app/models/customer.rb b/spec/example_app/app/models/customer.rb index bfc796ad14..e4c8d14658 100644 --- a/spec/example_app/app/models/customer.rb +++ b/spec/example_app/app/models/customer.rb @@ -4,6 +4,11 @@ class Customer < ActiveRecord::Base validates :name, presence: true validates :email, presence: true + KINDS = [ + :standard, + :vip, + ] + def lifetime_value orders.map(&:total_price).reduce(0, :+) end diff --git a/spec/example_app/db/migrate/20160119024340_add_kind_to_customer.rb b/spec/example_app/db/migrate/20160119024340_add_kind_to_customer.rb new file mode 100644 index 0000000000..3db41446c0 --- /dev/null +++ b/spec/example_app/db/migrate/20160119024340_add_kind_to_customer.rb @@ -0,0 +1,5 @@ +class AddKindToCustomer < ActiveRecord::Migration + def change + add_column :customers, :kind, :string, null: false, default: "standard" + end +end diff --git a/spec/example_app/db/schema.rb b/spec/example_app/db/schema.rb index be04bf567d..715366c3e5 100644 --- a/spec/example_app/db/schema.rb +++ b/spec/example_app/db/schema.rb @@ -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" @@ -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| diff --git a/spec/features/edit_page_spec.rb b/spec/features/edit_page_spec.rb index 8083933ffe..972b847e85 100644 --- a/spec/features/edit_page_spec.rb +++ b/spec/features/edit_page_spec.rb @@ -35,4 +35,26 @@ 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") + + click_on "Edit" + + select "standard", from: "Kind" + + click_on "Update Customer" + + expect(page).to have_content("KIND") + expect(page).to have_content("standard") + end end