Revision bbf64d99
Added by Joseph Magen over 10 years ago
app/controllers/api/v2/base_controller.rb | ||
---|---|---|
|
||
before_filter :root_node_name, :only => :index
|
||
before_render :get_metadata, :only => :index
|
||
before_filter :setup_has_many_params, :only => [:create, :update]
|
||
layout 'api/v2/layouts/index_layout', :only => :index
|
||
|
||
def root_node_name
|
||
... | ... | |
else
|
||
@page = 1
|
||
end
|
||
end
|
||
|
||
def setup_has_many_params
|
||
params.each do |k,v|
|
||
if v.kind_of?(Array)
|
||
magic_method_ids = "#{k.singularize}_ids"
|
||
magic_method_names = "#{k.singularize}_names"
|
||
if resource_class.instance_methods.map(&:to_s).include?(magic_method_ids) && v.any? && v.all? { |a| a.keys.include?("id") }
|
||
params[controller_name.singularize][magic_method_ids] = v.map { |a| a["id"] }
|
||
elsif resource_class.instance_methods.map(&:to_s).include?(magic_method_names) && v.any? && v.all? { |a| a.keys.include?("name") }
|
||
params[controller_name.singularize][magic_method_names] = v.map { |a| a["name"] }
|
||
end
|
||
end
|
||
end
|
||
end
|
||
|
||
end
|
app/controllers/api/v2/override_values_controller.rb | ||
---|---|---|
end
|
||
end
|
||
|
||
# overwrite Api::BaseController
|
||
def resource_class
|
||
LookupValue
|
||
end
|
||
|
||
end
|
||
end
|
||
end
|
app/controllers/api/v2/smart_class_parameters_controller.rb | ||
---|---|---|
render 'api/v2/smart_class_parameters/destroy'
|
||
end
|
||
|
||
# overwrite Api::BaseController
|
||
def resource_class
|
||
LookupKey
|
||
end
|
||
|
||
end
|
||
end
|
||
end
|
app/controllers/api/v2/smart_variables_controller.rb | ||
---|---|---|
render 'api/v2/smart_variables/destroy'
|
||
end
|
||
|
||
# overwrite Api::BaseController
|
||
def resource_class
|
||
LookupKey
|
||
end
|
||
|
||
end
|
||
end
|
||
end
|
app/models/lookup_key.rb | ||
---|---|---|
alias_attribute :variable_type, :key_type
|
||
alias_attribute :override_value_order, :path
|
||
alias_attribute :override_values_count, :lookup_values_count
|
||
alias_attribute :override_values, :lookup_values
|
||
alias_attribute :override_value_ids, :lookup_value_ids
|
||
|
||
# to prevent errors caused by find_resource from override_values controller
|
||
def self.find_by_name(str)
|
app/models/puppetclass.rb | ||
---|---|---|
validates :name, :uniqueness => true, :presence => true, :format => {:with => /\A(\S+\s?)+\Z/, :message => N_("can't be blank or contain white spaces.") }
|
||
audited :allow_mass_assignment => true
|
||
|
||
alias_attribute :smart_variables, :lookup_keys
|
||
alias_attribute :smart_variable_ids, :lookup_key_ids
|
||
alias_attribute :smart_class_parameters, :class_params
|
||
alias_attribute :smart_class_parameter_ids, :class_param_ids
|
||
|
||
default_scope lambda { order('puppetclasses.name') }
|
||
|
||
scoped_search :on => :name, :complete_value => :true
|
test/functional/api/v2/operatingsystems_controller_test.rb | ||
---|---|---|
assert_response :success
|
||
end
|
||
|
||
test "should update associated architectures by ids" do
|
||
os = operatingsystems(:redhat)
|
||
puts
|
||
assert_difference('os.architectures.count') do
|
||
put :update, { :id => operatingsystems(:redhat).to_param, :operatingsystem => { },
|
||
:architectures => [{ :id => architectures(:x86_64).id }, { :id => architectures(:sparc).id } ]
|
||
}
|
||
end
|
||
assert_response :success
|
||
end
|
||
|
||
test "should update associated architectures by name" do
|
||
os = operatingsystems(:redhat)
|
||
puts
|
||
assert_difference('os.architectures.count') do
|
||
put :update, { :id => operatingsystems(:redhat).to_param, :operatingsystem => { },
|
||
:architectures => [{ :name => architectures(:x86_64).name }, { :name => architectures(:sparc).name } ]
|
||
}
|
||
end
|
||
assert_response :success
|
||
end
|
||
|
||
end
|
Also available in: Unified diff
fixes #3693 - API v2 - accept GET json format of object in PUT/POST requests to add/remove has_many associations