foreman/app/controllers/puppetclasses_controller.rb @ acfbc458
c6eee281 | Ohad Levy | class PuppetclassesController < ApplicationController
|
|
1c4e9d45 | Ohad Levy | include Foreman::Controller::Environments
|
|
ac7fe332 | Ohad Levy | include Foreman::Controller::AutoCompleteSearch
|
|
acfbc458 | Marek Hulan | before_filter :find_by_name, :only => [:edit, :update, :destroy]
|
|
31207a31 | Ohad Levy | before_filter :setup_search_options, :only => :index
|
|
fb95b3c7 | Joseph Mitchell Magen | before_filter :reset_redirect_to_url, :only => :index
|
|
before_filter :store_redirect_to_url, :only => :edit
|
|||
bee8f73b | Ohad Levy | ||
af6e2624 | Ohad Levy | def index
|
|
acfbc458 | Marek Hulan | @puppetclasses = resource_base.search_for(params[:search], :order => params[:order]).includes(:environments, :hostgroups).paginate(:page => params[:page])
|
|
9e08f306 | Dmitri Dolguikh | @host_counter = Host.group(:puppetclass_id).joins(:puppetclasses).where(:puppetclasses => {:id => @puppetclasses.collect(&:id)}).count
|
|
f2c78d4a | Joseph Magen | @keys_counter = Puppetclass.joins(:class_params).select('distinct environment_classes.lookup_key_id').group(:name).count
|
|
acfbc458 | Marek Hulan | @hostgroups_authorizer = Authorizer.new(User.current, :collection => HostgroupClass.find_all_by_puppetclass_id(@puppetclasses.map(&:id)).compact.uniq.map(&:hostgroup_id))
|
|
6e50fa1d | Ohad Levy | end
|
|
af6e2624 | Ohad Levy | ||
def new
|
|||
@puppetclass = Puppetclass.new
|
|||
end
|
|||
def create
|
|||
@puppetclass = Puppetclass.new(params[:puppetclass])
|
|||
if @puppetclass.save
|
|||
bfbf7ed8 | Lukas Zapletal | notice _("Successfully created puppetclass.")
|
|
af6e2624 | Ohad Levy | redirect_to puppetclasses_url
|
|
else
|
|||
render :action => 'new'
|
|||
end
|
|||
end
|
|||
def edit
|
|||
end
|
|||
def update
|
|||
if @puppetclass.update_attributes(params[:puppetclass])
|
|||
bfbf7ed8 | Lukas Zapletal | notice _("Successfully updated puppetclass.")
|
|
fb95b3c7 | Joseph Mitchell Magen | redirect_back_or_default(puppetclasses_url)
|
|
af6e2624 | Ohad Levy | else
|
|
render :action => 'edit'
|
|||
end
|
|||
end
|
|||
def destroy
|
|||
if @puppetclass.destroy
|
|||
bfbf7ed8 | Lukas Zapletal | notice _("Successfully destroyed puppetclass.")
|
|
af6e2624 | Ohad Levy | else
|
|
754b1a01 | Ohad Levy | error @puppetclass.errors.full_messages.join("<br/>")
|
|
af6e2624 | Ohad Levy | end
|
|
redirect_to puppetclasses_url
|
|||
end
|
|||
8bfaec2a | Joseph Mitchell Magen | # form AJAX methods
|
|
def parameters
|
|||
puppetclass = Puppetclass.find(params[:id])
|
|||
0f9df583 | Amos Benari | render :partial => "puppetclasses/class_parameters", :locals => {
|
|
:puppetclass => puppetclass,
|
|||
0ba99bba | Joseph Mitchell Magen | :obj => get_host_or_hostgroup}
|
|
8bfaec2a | Joseph Mitchell Magen | end
|
|
private
|
|||
0ba99bba | Joseph Mitchell Magen | def get_host_or_hostgroup
|
|
# params['host_id'] = 'null' if NEW since hosts/form and hostgroups/form has data-id="null"
|
|||
if params['host_id'] == 'null'
|
|||
@obj = Host::Managed.new(params['host']) if params['host']
|
|||
@obj ||= Hostgroup.new(params['hostgroup']) if params['hostgroup']
|
|||
f591528c | Amos Benari | else
|
|
0ba99bba | Joseph Mitchell Magen | if params['host']
|
|
@obj = Host::Base.find(params['host_id'])
|
|||
unless @obj.kind_of?(Host::Managed)
|
|||
@obj = @obj.becomes(Host::Managed)
|
|||
@obj.type = "Host::Managed"
|
|||
end
|
|||
# puppetclass_ids is removed since it causes an insert on host_classes before form is submitted
|
|||
@obj.attributes = params['host'].except!(:puppetclass_ids)
|
|||
elsif params['hostgroup']
|
|||
# hostgroup.id is assigned to params['host_id'] by host_edit.js#load_puppet_class_parameters
|
|||
@obj = Hostgroup.find(params['host_id'])
|
|||
# puppetclass_ids is removed since it causes an insert on hostgroup_classes before form is submitted
|
|||
@obj.attributes = params['hostgroup'].except!(:puppetclass_ids)
|
|||
end
|
|||
f591528c | Amos Benari | end
|
|
0ba99bba | Joseph Mitchell Magen | @obj
|
|
8bfaec2a | Joseph Mitchell Magen | end
|
|
fb95b3c7 | Joseph Mitchell Magen | def reset_redirect_to_url
|
|
session[:redirect_to_url] = nil
|
|||
end
|
|||
def store_redirect_to_url
|
|||
session[:redirect_to_url] ||= request.referer
|
|||
end
|
|||
def redirect_back_or_default(default)
|
|||
redirect_to(session[:redirect_to_url] || default)
|
|||
session[:redirect_to_url] = nil
|
|||
end
|
|||
39558b72 | Dmitri Dolguikh | def find_by_name
|
|
not_found and return if params[:id].blank?
|
|||
acfbc458 | Marek Hulan | pc = resource_base.includes(:class_params => [:environment_classes, :environments, :lookup_values])
|
|
a387cbcb | Dominic Cleal | @puppetclass = (params[:id] =~ /\A\d+\Z/) ? pc.find(params[:id]) : pc.find_by_name(params[:id])
|
|
39558b72 | Dmitri Dolguikh | not_found and return unless @puppetclass
|
|
end
|
|||
acfbc458 | Marek Hulan | ||
6e50fa1d | Ohad Levy | end
|