Skip to content

Commit

Permalink
[api] Refactor find_attribute function
Browse files Browse the repository at this point in the history
There is no need to use string where clauses
  • Loading branch information
coolo committed Jun 26, 2018
1 parent 63767e4 commit 5a7eaf7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
27 changes: 11 additions & 16 deletions src/api/app/mixins/has_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,16 @@ def store_attribute(namespace, name, values, issues, binary = nil)
end

def find_attribute(namespace, name, binary = nil)
logger.debug "find_attribute for #{namespace}:#{name}"
raise AttributeFindError, 'Namespace must be given' if namespace.nil?
raise AttributeFindError, 'Name must be given' if name.nil?
if binary
if is_a? Project
raise AttributeFindError, 'binary packages are not allowed in project attributes'
end
# rubocop:disable Metrics/LineLength
a = attribs.joins(attrib_type: :attrib_namespace).where('attrib_types.name = ? and attrib_namespaces.name = ? AND attribs.binary = ?', name, namespace, binary).first
else
a = attribs.nobinary.joins(attrib_type: :attrib_namespace).where('attrib_types.name = ? and attrib_namespaces.name = ?', name, namespace).first
# rubocop:enable Metrics/LineLength
raise AttributeFindError, 'Namespace must be given' unless namespace
raise AttributeFindError, 'Name must be given' unless name
if is_a?(Project) && binary
raise AttributeFindError, 'binary packages are not allowed in project attributes'
end
a = attribs.find(a.id) if a && a.readonly? # FIXME: joins make things read only
a
query = attribs.joins(attrib_type: :attrib_namespace)
query = query.where(attrib_types: { name: name },
binary: binary,
attrib_namespaces: { name: namespace })
query.readonly(false).first
end

def render_attribute_axml(params = {})
Expand All @@ -98,8 +93,8 @@ def render_main_attributes(builder, params)
done = {}
attribs.each do |attr|
type_name = attr.attrib_type.attrib_namespace.name + ':' + attr.attrib_type.name
next if params[:name] && !(attr.attrib_type.name == params[:name])
next if params[:namespace] && !(attr.attrib_type.attrib_namespace.name == params[:namespace])
next if params[:name] && attr.attrib_type.name != params[:name]
next if params[:namespace] && attr.attrib_type.attrib_namespace.name != params[:namespace]
next if params[:binary] && attr.binary != params[:binary]
next if params[:binary] == '' && attr.binary != '' # switch between all and NULL binary
done[type_name] = 1 unless attr.binary
Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/attrib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Attrib < ApplicationRecord

#### Callbacks macros: before_save, after_save, etc.
#### Scopes (first the default_scope macro if is used)
scope :nobinary, -> { where(binary: nil) }

#### Validations macros
validates_associated :values
Expand Down

0 comments on commit 5a7eaf7

Please sign in to comment.