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 collection option for resource relations #2084

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
1 change: 1 addition & 0 deletions app/views/alchemy/admin/resources/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<% resource_handler.editable_attributes.each do |attribute| %>
<% if relation = attribute[:relation] %>
<%= f.association relation[:name].to_sym,
collection: relation[:collection],
label_method: relation[:attr_method],
include_blank: Alchemy.t(:blank, scope: 'resources.relation_select'),
input_html: {class: 'alchemy_selectbox'} %>
Expand Down
16 changes: 13 additions & 3 deletions lib/alchemy/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ def resource_relation_type(column_name)
resource_relation(column_name).try(:[], :attr_type)
end

def resource_relation_class(association)
class_name = association.options[:class_name] || association.name.to_s.classify
class_name.constantize
end

def resource_column_type(col)
resource_relation_type(col.name) || (col.try(:array) ? :array : col.type)
end
Expand All @@ -296,10 +301,15 @@ def resource_relation(column_name)
def map_relations
self.resource_relations = {}
model.alchemy_resource_relations.each do |name, options|
name = name.to_s.gsub(/_id$/, "") # ensure that we don't have an id
association = association_from_relation_name(name)
relation_name = name.to_s.gsub(/_id$/, "") # ensure that we don't have an id
association = association_from_relation_name(relation_name)
foreign_key = association.options[:foreign_key] || "#{association.name}_id".to_sym
resource_relations[foreign_key] = options.merge(model_association: association, name: name)
collection = options[:collection] || resource_relation_class(association).all
resource_relations[foreign_key] = options.merge(
model_association: association,
name: relation_name,
collection: collection,
)
end
end

Expand Down
20 changes: 20 additions & 0 deletions spec/libraries/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,26 @@ module Alchemy
expect(relation.keys).to include(:name)
expect(relation[:name]).to eq("location")
end

it "stores an ActiveRecord::Relation scope for all records in a collection key" do
expect(Location).to receive(:all).and_call_original
expect(resource.resource_relations[:location_id][:collection]).to be_a(ActiveRecord::Relation)
end

context "when collection gets passed with a specific ActiveRecord::Relation" do
before do
allow(Event).to receive(:alchemy_resource_relations) do
{
location: {attr_method: "name", attr_type: :string, collection: Location.where(name: "foo")},
}
end
end

it "stores the given ActiveRecord::Relation scope in a collection key" do
expect(resource.resource_relations[:location_id][:collection].where_values_hash).to eq({"name" => "foo"})
expect(resource.resource_relations[:location_id][:collection]).to be_a(ActiveRecord::Relation)
end
end
end

describe "#model_associations" do
Expand Down