Skip to content

Commit

Permalink
Merge pull request #2084 from robinboening/feature_resource_relation_…
Browse files Browse the repository at this point in the history
…collection

Add collection option for resource relations
  • Loading branch information
tvdeyen authored May 6, 2021
2 parents fcdc7cc + cfed24d commit 32eab15
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
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

0 comments on commit 32eab15

Please sign in to comment.