Skip to content

Commit

Permalink
Add support for selectable strings on edit pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Jberlinsky committed Jan 19, 2016
1 parent d0bc1c7 commit 2c4ed97
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 2 deletions.
26 changes: 26 additions & 0 deletions app/views/fields/select/_form.html.erb
Original file line number Diff line number Diff line change
@@ -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
%>

<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)
) %>
</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.presence %>
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.presence %>
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/fields/image"
require "administrate/fields/number"
require "administrate/fields/polymorphic"
require "administrate/fields/select"
require "administrate/fields/string"
require "administrate/fields/text"

Expand Down
21 changes: 21 additions & 0 deletions lib/administrate/fields/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
3 changes: 2 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,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
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 = [
:standard,
:vip,
]

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
22 changes: 22 additions & 0 deletions spec/features/edit_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2c4ed97

Please sign in to comment.