Revision 2d5e52d9
Added by Ondřej Pražák almost 6 years ago
app/models/concerns/template_tax.rb | ||
---|---|---|
module TemplateTax
|
||
extend ActiveSupport::Concern
|
||
|
||
module ClassMethods
|
||
def taxonomy_exportable
|
||
{
|
||
:organizations => method(:assigned_organization_titles),
|
||
:locations => method(:assigned_location_titles)
|
||
}
|
||
end
|
||
|
||
def assigned_organization_titles(template, user = User.current)
|
||
template.organizations.select { |org| user.my_organizations.include?(org) }.map(&:title)
|
||
end
|
||
|
||
def assigned_location_titles(template, user = User.current)
|
||
template.locations.select { |loc| user.my_locations.include?(loc) }.map(&:title)
|
||
end
|
||
end
|
||
end
|
app/models/provisioning_template.rb | ||
---|---|---|
# these can't be shared in parent class, scoped search can't handle STI properly
|
||
# tested with scoped_search 3.2.0
|
||
include Taxonomix
|
||
include TemplateTax
|
||
scoped_search :on => :name, :complete_value => true, :default_order => true
|
||
scoped_search :on => :locked, :complete_value => {:true => true, :false => false}
|
||
scoped_search :on => :snippet, :complete_value => {:true => true, :false => false}
|
||
... | ... | |
scoped_search :relation => :hostgroups, :on => :name, :rename => :hostgroup, :complete_value => true
|
||
scoped_search :relation => :template_kind, :on => :name, :rename => :kind, :complete_value => true
|
||
|
||
attr_exportable :kind => Proc.new { |template| template.template_kind.try(:name) },
|
||
:oses => Proc.new { |template| template.operatingsystems.map(&:name).uniq }
|
||
attr_exportable({
|
||
:kind => Proc.new { |template| template.template_kind.try(:name) },
|
||
:oses => Proc.new { |template| template.operatingsystems.map(&:name).uniq }
|
||
}.merge(taxonomy_exportable))
|
||
|
||
dirty_has_many_associations :template_combinations, :os_default_templates, :operatingsystems
|
||
|
app/models/ptable.rb | ||
---|---|---|
# these can't be shared in parent class, scoped search can't handle STI properly
|
||
# tested with scoped_search 3.2.0
|
||
include Taxonomix
|
||
include TemplateTax
|
||
scoped_search :on => :name, :complete_value => true, :default_order => true
|
||
scoped_search :on => :locked, :complete_value => {:true => true, :false => false}
|
||
scoped_search :on => :snippet, :complete_value => {:true => true, :false => false}
|
||
... | ... | |
|
||
alias_attribute :layout, :template
|
||
|
||
attr_exportable :os_family
|
||
attr_exportable :os_family, taxonomy_exportable
|
||
|
||
dirty_has_many_associations :operatingsystems
|
||
|
test/controllers/api/v2/ptables_controller_test.rb | ||
---|---|---|
get :export, params: { :id => ptable.to_param }
|
||
assert_response :success
|
||
assert_equal 'text/plain', response.content_type
|
||
User.current = users(:admin)
|
||
assert_equal ptable.to_erb, response.body
|
||
end
|
||
|
test/controllers/ptables_controller_test.rb | ||
---|---|---|
get :export, params: { :id => @ptable.to_param }, session: set_session_user
|
||
assert_response :success
|
||
assert_equal 'text/plain', response.content_type
|
||
User.current = users(:admin)
|
||
assert_equal @ptable.to_erb, response.body
|
||
end
|
||
|
test/models/template_test.rb | ||
---|---|---|
@loc1 = FactoryBot.create(:location)
|
||
@loc2 = FactoryBot.create(:location)
|
||
@loc3 = FactoryBot.create(:location)
|
||
@loc4 = FactoryBot.create(:location, :ancestry => @loc3.id.to_s)
|
||
end
|
||
|
||
test 'it ignores locations if none was set in metadata and sets current location' do
|
||
... | ... | |
refute_includes @template.location_ids, @loc3.id
|
||
end
|
||
|
||
test 'it properly imports nested locations' do
|
||
@template.instance_variable_set '@importing_metadata', { 'locations' => [@loc4.title] }
|
||
@template.send(:import_locations, :associate => 'always')
|
||
assert_includes @template.location_ids, @loc4.id
|
||
end
|
||
|
||
test 'unknown locations are ignored' do
|
||
@template.instance_variable_set '@importing_metadata', { 'locations' => ['not_available'] }
|
||
assert_nothing_raised { @template.send(:import_oses, :associate => 'always') }
|
||
... | ... | |
assert_equal "cannot be used, please choose another", template.errors.messages[:name].first
|
||
end
|
||
end
|
||
|
||
describe 'taxonomies in metadata' do
|
||
test 'should add taxonomies to metadata' do
|
||
org = FactoryBot.create(:organization, :name => 'TemplateOrg')
|
||
loc = FactoryBot.create(:location, :name => 'TemplateLoc')
|
||
provisioning_template = FactoryBot.create(:provisioning_template,
|
||
:name => 'exported_template',
|
||
:organizations => [org],
|
||
:locations => [loc])
|
||
assert_equal org.title, provisioning_template.to_export["organizations"].first
|
||
assert_equal loc.title, provisioning_template.to_export["locations"].first
|
||
end
|
||
end
|
||
end
|
||
end
|
Also available in: Unified diff
Fixes #23896 - Update taxonomies when refreshing metadata