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

Support AR enums in resource models #2210

Merged
merged 1 commit into from
Apr 1, 2022
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
5 changes: 5 additions & 0 deletions app/views/alchemy/admin/resources/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
input_html: {class: 'alchemy_selectbox'} %>
<% elsif attribute[:type].in? %i[date time datetime] %>
<%= f.datepicker attribute[:name], resource_attribute_field_options(attribute) %>
<% elsif attribute[:enum].present? %>
<%= f.input attribute[:name],
collection: attribute[:enum],
include_blank: Alchemy.t(:blank, scope: 'resources.relation_select'),
input_html: {class: 'alchemy_selectbox'} %>
<% else %>
<%= f.input attribute[:name], resource_attribute_field_options(attribute) %>
<% end %>
Expand Down
17 changes: 16 additions & 1 deletion lib/alchemy/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,25 @@ def attributes
name: col.name,
type: resource_column_type(col),
relation: resource_relation(col.name),
}.delete_if { |_k, v| v.nil? }
enum: enum_values_collection_for_select(col.name),
}.delete_if { |_k, v| v.blank? }
end.compact
end

def enum_values_collection_for_select(column_name)
enum = model.defined_enums[column_name]
return if enum.blank?

enum.keys.map do |key|
[
::I18n.t(key, scope: [
:activerecord, :attributes, model.model_name.i18n_key, "#{column_name}_values"
], default: key.humanize),
key,
]
end
end

def sorted_attributes
@_sorted_attributes ||= attributes.
sort_by { |attr| attr[:name] == "name" ? 0 : 1 }.
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ class Event < ActiveRecord::Base

validates_presence_of :name
belongs_to :location

enum event_type: {
expo: 0,
workshop: 1,
}

before_destroy :abort_if_name_is_undestructible

scope :starting_today, -> { where(starts_at: Time.current.at_midnight..Date.tomorrow.at_midnight) }
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ en:
attributes:
event:
tag_names: Tags
event_type: Type
event_type_values:
expo: exposition
workshop: workshop
location:
tag_names: Tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddEventTypeToEvents < ActiveRecord::Migration[5.2]
def change
add_column :events, :event_type, :integer, null: false, default: 0
end
end
3 changes: 2 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_05_08_091432) do
ActiveRecord::Schema.define(version: 2021_11_05_175532) do

create_table "alchemy_attachments", force: :cascade do |t|
t.string "name"
Expand Down Expand Up @@ -364,6 +364,7 @@
t.integer "location_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "event_type", default: 0, null: false
end

create_table "gutentag_taggings", force: :cascade do |t|
Expand Down
10 changes: 9 additions & 1 deletion spec/features/admin/resources_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,15 @@
it "should have a select box for associated models" do
visit "/admin/events/new"
within("form") do
expect(page).to have_selector("select")
expect(page).to have_selector("select#event_location_id")
end
end

it "should have a select box for enums values" do
visit "/admin/events/new"

within("form") do
expect(page).to have_selector("select#event_event_type")
end
end

Expand Down