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

Dynamic select field #1589

Merged
Merged
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
3 changes: 2 additions & 1 deletion docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ objects to display as.

**Field::Select**

`:collection` - Specify the array or range to select from. Defaults to `[]`.
`:collection` - Specify the options shown on the select field. It accept either
an array or an object responding to `:call`. Defaults to `[]`.

`:searchable` - Specify if the attribute should be considered when searching.
Default is `true`.
Expand Down
5 changes: 4 additions & 1 deletion lib/administrate/field/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ def selectable_options
private

def collection
@collection ||= options.fetch(:collection, [])
maybe_proc = options.fetch(:collection, [])
return maybe_proc.call if maybe_proc.respond_to? :call

@collection ||= maybe_proc
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/example_app/app/dashboards/product_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ProductDashboard < Administrate::BaseDashboard
:description,
:image_url,
:product_meta_tag,
:release_year,
]

ATTRIBUTE_TYPES = {
Expand All @@ -17,6 +18,9 @@ class ProductDashboard < Administrate::BaseDashboard
name: Field::String,
price: Field::Number.with_options(prefix: "$", decimals: 2),
product_meta_tag: Field::HasOne,
release_year: Field::Select.with_options(
collection: -> { (Time.current.year - 10)..Time.current.year },
),
}

COLLECTION_ATTRIBUTES = ATTRIBUTES
Expand Down
5 changes: 5 additions & 0 deletions spec/example_app/app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class Product < ApplicationRecord
validates :image_url, presence: true
validates :name, presence: true
validates :price, presence: true
validates :release_year,
numericality: {
less_than_or_equal_to: ->(_product) { Time.current.year },
},
allow_blank: true
validates :slug, uniqueness: true
validate :valid_slug

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddReleaseYearToProducts < ActiveRecord::Migration[6.0]
def change
add_column :products, :release_year, :integer, limit: 2
end
end
13 changes: 7 additions & 6 deletions spec/example_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
# This file is the source Rails uses to define your schema when running `rails
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180525115059) do
ActiveRecord::Schema.define(version: 2020_03_26_202615) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -100,6 +100,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug", null: false
t.integer "release_year", limit: 2
t.index ["slug"], name: "index_products_on_slug", unique: true
end

Expand Down
1 change: 1 addition & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"https://cdn.recombu.com/mobile/images/news/M11370/1264769196_w670.jpg"
end
product_meta_tag
release_year { [2018, 2019, 2020].sample }
end

factory :product_meta_tag do
Expand Down
14 changes: 14 additions & 0 deletions spec/features/products_form_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "rails_helper"

describe "product form has_one relationship" do
include ActiveSupport::Testing::TimeHelpers

it "saves product and meta tag data correctly" do
visit new_admin_product_path

Expand All @@ -21,6 +23,18 @@
)
end

it "have dynamic release_year" do
visit new_admin_product_path
current_year = Time.current.year
expect(page).to have_select("Release year", with_options: [current_year])

travel_to(1.year.ago) do
visit new_admin_product_path
expect(page).
not_to have_select("Release year", with_options: [current_year])
end
end

it "edits product and meta tag data correctly" do
product = create(:product)

Expand Down
5 changes: 5 additions & 0 deletions spec/models/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
it { should validate_presence_of(:name) }
it { should validate_presence_of(:price) }

it do
should validate_numericality_of(:release_year).
is_less_than_or_equal_to(Time.now.year)
end

it "should not allow names that produce empty slugs" do
product = build(:product, name: "???")

Expand Down