Skip to content

Commit

Permalink
[api] Use better way to set container object for attributes
Browse files Browse the repository at this point in the history
Before we had this logic in our controller. This code should
belong to the model and cleans up the controller.
  • Loading branch information
Manuel Schnitzer committed Mar 8, 2017
1 parent 39f911d commit de3cdc0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/api/app/controllers/attribute_controller.rb
Expand Up @@ -243,12 +243,7 @@ def cmd_attribute
attrib_type = AttribType.find_by_namespace_and_name!(attr.value("namespace"), attr.value("name"))
attrib = Attrib.new(attrib_type: attrib_type)

if @attribute_container.is_a?(Project)
attrib.project = @attribute_container
else
attrib.package = @attribute_container
end

attrib.container = @attribute_container
authorize attrib, :create?
end

Expand Down
8 changes: 8 additions & 0 deletions src/api/app/models/attrib.rb
Expand Up @@ -56,6 +56,14 @@ def container
end
end

def container=(container_object)
if container_object.is_a?(Project)
self.project = container_object
else
self.package = container_object
end
end

def project
if package
package.project
Expand Down
19 changes: 19 additions & 0 deletions src/api/spec/models/attrib_spec.rb
@@ -0,0 +1,19 @@
require 'rails_helper'

RSpec.describe Attrib do
let(:attribute) { create(:attrib, project: create(:project)) }
let(:project) { create(:project) }
let(:package) { create(:package) }

context "#container=" do
it "sets the project" do
attribute.container = project
expect(attribute.container).to be(project)
end

it "sets the package" do
attribute.container = package
expect(attribute.container).to be(package)
end
end
end

0 comments on commit de3cdc0

Please sign in to comment.