Skip to content
Closed
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: 1 addition & 2 deletions app/models/runtime/buildpack_lifecycle_data_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class BuildpackLifecycleDataModel < Sequel::Model(:buildpack_lifecycle_data)
one_to_many :buildpack_lifecycle_buildpacks,
class: '::VCAP::CloudController::BuildpackLifecycleBuildpackModel',
key: :buildpack_lifecycle_data_guid,
primary_key: :guid,
order: :id
primary_key: :guid
plugin :nested_attributes
nested_attributes :buildpack_lifecycle_buildpacks, destroy: true
add_association_dependencies buildpack_lifecycle_buildpacks: :destroy
Expand Down
3 changes: 1 addition & 2 deletions app/models/runtime/cnb_lifecycle_data_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class CNBLifecycleDataModel < Sequel::Model(:cnb_lifecycle_data)
one_to_many :buildpack_lifecycle_buildpacks,
class: '::VCAP::CloudController::BuildpackLifecycleBuildpackModel',
key: :cnb_lifecycle_data_guid,
primary_key: :guid,
order: :id
primary_key: :guid
plugin :nested_attributes
nested_attributes :buildpack_lifecycle_buildpacks, destroy: true
add_association_dependencies buildpack_lifecycle_buildpacks: :destroy
Expand Down
2 changes: 1 addition & 1 deletion app/models/services/service_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ServicePlan < Sequel::Model

add_association_dependencies service_plan_visibilities: :destroy

one_to_many(:orgs_visibility, clone: :service_plan_visibilities) { |ds| ds.select(:service_plan_id).distinct }
one_to_many(:orgs_visibility, clone: :service_plan_visibilities, order: :service_plan_id) { |ds| ds.select(:service_plan_id).distinct }

one_to_many :labels, class: 'VCAP::CloudController::ServicePlanLabelModel', key: :resource_guid, primary_key: :guid
add_association_dependencies labels: :destroy
Expand Down
8 changes: 6 additions & 2 deletions lib/sequel_plugins/vcap_relations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def one_through_one(name, opts={})
#
# See the default many_to_many implementation for a description of the args
# and return values.
def many_to_many(name, opts={})
def many_to_many(name, opts={}, &block)
opts[:order] ||= :id unless block

singular_name = name.to_s.singularize
ids_attr = "#{singular_name}_ids"
guids_attr = "#{singular_name}_guids"
Expand Down Expand Up @@ -93,7 +95,9 @@ def many_to_many(name, opts={})
#
# See the default one_to_many implementation for a description of the args
# and return values.
def one_to_many(name, opts={})
def one_to_many(name, opts={}, &block)
opts[:order] ||= :id unless block

singular_name = name.to_s.singularize
ids_attr = "#{singular_name}_ids"
guids_attr = "#{singular_name}_guids"
Expand Down
28 changes: 28 additions & 0 deletions spec/unit/lib/sequel_plugins/vcap_relations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ def define_model(name)
expect(@o.dogs).to be_empty
end

it 'orders by id by default' do
expect(@o.dogs_dataset.sql).to match(/ORDER BY .id.$/)
end

it 'allows overriding the default order' do
owner_klass.one_to_many :dogs, order: Sequel.desc(:id)

expect(@o.dogs_dataset.sql).to match(/ORDER BY .id. DESC$/)
end

it 'allows overriding the default order via dataset' do
expect(@o.dogs_dataset.order(:created_at).sql).to match(/ORDER BY .created_at.$/)
end

it 'adds a add_<relation> method that takes an object' do
d = dog_klass.create
@o.add_dog d
Expand Down Expand Up @@ -167,6 +181,20 @@ def define_model(name)
expect(@d1.names).to be_empty
end

it 'orders by id by default' do
expect(@d1.names_dataset.sql).to match(/ORDER BY .id.$/)
end

it 'allows overriding the default order' do
dog_klass.many_to_many :names, order: Sequel.desc(:id)

expect(@d1.names_dataset.sql).to match(/ORDER BY .id. DESC$/)
end

it 'allows overriding the default order via dataset' do
expect(@d1.names_dataset.order(:created_at).sql).to match(/ORDER BY .created_at.$/)
end

it 'adds a add_<relation> method that takes an object' do
@d1.add_name @n1
expect(@d1.names).to include(@n1)
Expand Down
Loading