Class: Katello::Api::V2::DebsController

Inherits:
ApiController
  • Object
show all
Includes:
Concerns::Api::V2::RepositoryContentController
Defined in:
app/controllers/katello/api/v2/debs_controller.rb

Instance Method Summary collapse

Methods inherited from ApiController

#empty_search_query?, #full_result_response, #scoped_search, #skip_session

Methods included from Rendering

#respond_for_async, #respond_for_bulk_async, #respond_for_create, #respond_for_destroy, #respond_for_index, #respond_for_show, #respond_for_status, #respond_for_update, #respond_with_template, #respond_with_template_collection, #respond_with_template_resource, #try_specific_collection_template, #try_specific_resource_template

Methods included from Katello::Api::Version2

#api_version

Instance Method Details

#auto_complete(search) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/katello/api/v2/debs_controller.rb', line 13

def auto_complete(search)
  page_size = Katello::Concerns::FilteredAutoCompleteSearch::PAGE_SIZE
  debs = Deb.in_repositories(@repositories)
  col = case search
        when 'name' then "#{Deb.table_name}.name"
        when 'arch' then "#{Deb.table_name}.architecture"
        end
  return render json: [] if col.blank?

  scope = debs.select(col).group(col).order(col).limit(page_size)
  scope = scope.where("#{col} ILIKE ?", "%#{params[:term]}%") if params[:term].present?
  render json: scope.pluck(Arel.sql(col))
end

#auto_complete_archObject



31
32
33
# File 'app/controllers/katello/api/v2/debs_controller.rb', line 31

def auto_complete_arch
  auto_complete('arch')
end

#auto_complete_nameObject



27
28
29
# File 'app/controllers/katello/api/v2/debs_controller.rb', line 27

def auto_complete_name
  auto_complete('name')
end

#available_for_content_view_version(version) ⇒ Object



95
96
97
# File 'app/controllers/katello/api/v2/debs_controller.rb', line 95

def available_for_content_view_version(version)
  version.available_debs
end

#custom_index_relation(collection) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/katello/api/v2/debs_controller.rb', line 99

def custom_index_relation(collection)
  applicable = ::Foreman::Cast.to_bool(params[:packages_restrict_applicable]) || params[:host_id]
  upgradable = ::Foreman::Cast.to_bool(params[:packages_restrict_upgradable])
  not_installed = ::Foreman::Cast.to_bool(params[:packages_restrict_not_installed])

  if upgradable
    collection = collection.installable_for_hosts(@hosts)
  elsif not_installed && params[:host_id]
    host = @hosts.first
    collection = Katello::Deb.deb_installable_for_host(host)
  elsif applicable
    collection = collection.applicable_to_hosts(@hosts)
  end

  collection
end

#default_sortObject



90
91
92
93
# File 'app/controllers/katello/api/v2/debs_controller.rb', line 90

def default_sort
  lambda { |query| query.default_sort }
  # %w(name asc)
end

#filter_by_content_view_filter(filter, collection) ⇒ Object



75
76
77
78
79
80
81
82
# File 'app/controllers/katello/api/v2/debs_controller.rb', line 75

def filter_by_content_view_filter(filter, collection)
  filtered_debs = []
  filter.deb_rules.each do |rule|
    filtered_debs += filter.query_debs_from_collection(collection, rule).pluck(:id)
  end

  collection.where(id: filter.applicable_debs.pluck(:id) & filtered_debs)
end

#filter_by_content_view_filter_rule(rule, collection) ⇒ Object



84
85
86
87
88
# File 'app/controllers/katello/api/v2/debs_controller.rb', line 84

def filter_by_content_view_filter_rule(rule, collection)
  filter = rule.filter
  filtered_debs = filter.query_debs_from_collection(collection, rule).pluck(:id)
  collection.where(id: filter.applicable_debs.pluck(:id) & filtered_debs)
end

#final_custom_index_relation(collection) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'app/controllers/katello/api/v2/debs_controller.rb', line 65

def final_custom_index_relation(collection)
  # :packages_restrict_latest is intended to filter the result set after all
  # other constraints have been applied, including the scoped_search
  # constraints.  If any constraints are applied after this, then a package
  # will not be returned if its latest version does not match those
  # constraints, even if an older version does match those constraints.
  collection = Katello::Deb.latest(collection) if ::Foreman::Cast.to_bool(params[:packages_restrict_latest])
  collection
end

#indexObject



51
52
53
# File 'app/controllers/katello/api/v2/debs_controller.rb', line 51

def index
  super
end

#thindexObject



55
56
57
58
59
60
61
62
63
# File 'app/controllers/katello/api/v2/debs_controller.rb', line 55

def thindex
  sort_by, sort_order, options = sort_options

  options[:select] = "DISTINCT ON (#{Deb.table_name}.name) #{Deb.table_name}.id, #{Deb.table_name}.name"
  final_relation = custom_index_relation(Deb.all)

  result = scoped_search(final_relation, sort_by, sort_order, options)
  respond_for_index(:collection => result, :template => "thindex")
end