# -*- encoding : utf-8 -*-
module KirguduBase
module Controllers
module BasicActions
################################################################################################################
######################################### Base and Ajax Modules ######################################
################################################################################################################
module Base
module IndexActions
def index
#raise self.entry_class
raise "No defined \"entry_class\" method for controller #{self.class}" if !self.respond_to?(:entry_class) && self.entry_class.nil?
#raise "Current User NULL" if @current_user.nil?
unless self.class.respond_to?(:get_kb_page)
raise "Method :get_kb_page must be implemented for controller '#{self.class}'"
end
local_page = self.class.get_kb_page(:index)
unless local_page
respond_to do |format|
format.html { render_html1 self.get_view_path_for_page_404 }
format.json { render_json_page_404 }
end
return
end
filters = self.get_filters_for_list(local_page.lists[:entries_list], params)
self.fill_obligatory_filters(filters)
options = {
logger: string_logger
}
local_filters_data = self.load_data_for_filters_controls(local_page.lists[:entries_list], params)
local_entries_list = self.entry_class.get_entries(nil, filters, options)
respond_to do |format|
format.html { render_html1 self.get_view_path_for_page_index, locals: {entries_list: local_entries_list, page: local_page, filters_data: local_filters_data} }
format.json { render_json_ok(local_entries_list) }
end
end
end
module ShowActions
def show
unless self.class.respond_to?(:get_kb_page)
raise "Method :get_kb_page must be implemented for controller '#{self.class}'"
end
local_page = self.class.get_kb_page(:show)
if local_page.nil?
respond_to do |format|
format.html { render_html1 self.get_view_path_for_page_404 }
format.json { render_json_page_404 }
end
return
end
#::ChupakabraTools::ClassHelper.get_controller_hierarchy(self.class).each do |klass|
# parent_page = klass.get_kb_page(:show) if klass.respond_to?(:get_kb_page)
#
# parent_page.menus.each_pair do |key, parent_menu|
# menu = local_page.menus[parent_menu.name]
# if menu
# parent_menu.items.each do |menu_item|
# menu.items << menu_item
# end
# end
# end if parent_page
#end
filters = {}
self.fill_obligatory_filters(filters)
local_entry = self.entry_class.get_entry_by_id(params[:id], filters, {logger: string_logger})
unless local_entry
respond_to do |format|
flash[:error] = I18n.t("#{self.to_i18n}.messages.not_found", entry_id: params[:id])
format.html { render_html1 get_not_found_view_path,
locals: {
entry: local_entry
}
}
format.json { render json: local_entry.errors, status: :unprocessable_entity }
end
return
end
respond_to do |format|
format.html { render_html1 self.get_view_path_for_page_show, locals: {entry: local_entry, page: local_page} }
format.json { render json: local_entry }
end
end
end
module NewActions
def new
unless self.class.respond_to?(:get_kb_page)
raise "Method :get_kb_page must be implemented for controller '#{self.class}'"
end
local_page = self.class.get_kb_page(:new)
unless local_page
respond_to do |format|
format.html { render_html1 self.get_view_path_for_page_404 }
format.json { render_json_page_404 }
end
return
end
local_entry = entry_class.new
# loading data for select-like controls
local_controls_data = self.load_data_for_entry_controls(local_page.forms[:changes], local_entry)
respond_to do |format|
format.html { render_html1 self.get_view_path_for_page_new,
locals:
{
entry: local_entry,
page: local_page,
controls_data: local_controls_data
}
}
format.json { render json: local_entry }
end
end
def create
unless self.class.respond_to?(:get_kb_page)
raise "Method :get_kb_page must be implemented for controller '#{self.class}'"
end
local_page = self.class.get_kb_page(:new)
unless local_page
respond_to do |format|
format.html { render_html1 self.get_view_path_for_page_404 }
format.json { render_json_page_404 }
end
return
end
strong_params = {}.merge(self.class_strong_params(local_page.forms[:changes]))
self.fill_obligatory_filters(strong_params)
local_entry = self.entry_class.new(strong_params)
self.process_transaction_injections(local_entry, :create, :before_transaction, strong_params)
transaction_is_ok = true
ActiveRecord::Base.transaction do
#raise "In Transaction Before Create"
unless self.process_transaction_injections(local_entry, :create, :before_save, strong_params)
transaction_is_ok = false
raise ActiveRecord::Rollback
end
if local_entry.save
unless process_transaction_injections(local_entry, :create, :after_save, strong_params)
transaction_is_ok = false
raise ActiveRecord::Rollback
end
else
transaction_is_ok = false
raise ActiveRecord::Rollback
end
end
self.process_transaction_injections(local_entry, :create, :after_transaction, strong_params)
respond_to do |format|
if transaction_is_ok
flash[:success] = I18n.t("#{self.to_i18n}.messages.entry_created", entry_name: local_entry.to_s)
format.html { render_redirect url_for(action: :show, id: local_entry) }
format.json { render json: local_entry, status: :created, location: local_entry }
else
# loading data for select-like controls
local_controls_data = self.load_data_for_entry_controls(local_page.forms[:changes], local_entry)
local_entry.errors[:form] << I18n.t("#{self.to_i18n}.messages.create_error")
format.html { render_html1 self.get_view_path_for_page_new,
locals:
{
entry: local_entry,
page: local_page,
controls_data: local_controls_data
}
}
format.json { render json: local_entry.errors, status: :unprocessable_entity }
end
end
end
end
module EditActions
def edit
unless self.class.respond_to?(:get_kb_page)
raise "Method :get_kb_page must be implemented for controller '#{self.class}'"
end
local_page = self.class.get_kb_page(:edit)
unless local_page
respond_to do |format|
format.html { render_html1 get_view_path_for_page_404 }
format.json { render_json_page_404 }
end
return
end
filters = {}
self.fill_obligatory_filters(filters)
local_entry = self.entry_class.get_entry_by_id(params[:id], filters, {logger: string_logger})
unless local_entry
respond_to do |format|
flash[:error] = I18n.t("#{self.to_i18n}.messages.not_found", entry_id: params[:id])
format.html { render_html1 self.get_not_found_view_path, locals: {entry: local_entry} }
format.json { render json: local_entry.errors, status: :unprocessable_entity }
end
return
end
# loading data for select-like controls
local_controls_data = self.load_data_for_entry_controls(local_page.forms[:changes], local_entry)
respond_to do |format|
format.html { render_html1 self.get_view_path_for_page_edit,
locals:
{
entry: local_entry,
page: local_page,
controls_data: local_controls_data
}
}
format.json { render json: local_entry }
end
end
def update
unless self.class.respond_to?(:get_kb_page)
raise "Method :get_kb_page must be implemented for controller '#{self.class}'"
end
local_page = self.class.get_kb_page(:edit)
unless local_page
respond_to do |format|
format.html { render_html1 self.get_view_path_for_page_404 }
format.json { render_json_page_404 }
end
return
end
local_entry = self.entry_class.get_entry_by_id(params[:id], nil, {logger: string_logger})
# Render NOT FOUND Form
unless local_entry
respond_to do |format|
flash[:error] = I18n.t("#{self.to_i18n}.messages.not_found", entry_id: params[:id])
format.html { render_html1 self.get_not_found_view_path, locals: {entry: local_entry} }
format.json { render json: nil, status: :unprocessable_entity }
end
return
end
strong_params = {}.merge(self.class_strong_params(local_page.forms[:changes]))
self.fill_obligatory_filters(strong_params)
self.process_transaction_injections(local_entry, :update, :before_transaction, strong_params)
transaction_is_ok = true
ActiveRecord::Base.transaction do
#raise "In Transaction Before Create"
unless self.process_transaction_injections(local_entry, :update, :before_save, strong_params)
transaction_is_ok = false
raise ActiveRecord::Rollback
end
if local_entry.update(strong_params)
unless process_transaction_injections(local_entry, :update, :after_save, strong_params)
transaction_is_ok = false
raise ActiveRecord::Rollback
end
else
transaction_is_ok = false
raise ActiveRecord::Rollback
end
end
self.process_transaction_injections(local_entry, :update, :after_transaction, strong_params)
respond_to do |format|
flash[:success] = I18n.t("#{self.to_i18n}.messages.manufacturer_updated", entry_name: local_entry.to_s)
if transaction_is_ok
format.html { render_redirect url_for(action: :show, id: local_entry) }
format.json { head :no_content }
else
# loading data for select-like controls
local_controls_data = self.load_data_for_entry_controls(local_page.forms[:changes], local_entry)
local_entry.errors[:form] << I18n.t("#{self.to_i18n}.messages.update_error")
format.html { render_html1 self.get_view_path_for_page_edit,
locals:
{
entry: local_entry,
page: local_page,
controls_data: local_controls_data
}
}
format.json { render json: local_entry.errors, status: :unprocessable_entity }
end
end
end
end
module DeleteActions
def delete
end
end
module ExportActions
def export
raise "No defined \"entry_class\" method for controller #{self.class}" if !self.respond_to?(:entry_class) || self.entry_class.nil?
raise "Current User NULL" if @current_user.nil?
filters = {}.merge(params)
options = {
logger: string_logger
}
#logger.info "OPTIONS FOR SEARCH: #{options.to_s}"
@entries_list = self.entry_class.get_entries(nil, filters, options)
export_type = params[:type]
unless export_type
end
export_options = {}
self.entry_class.export_types_set.each do |exp|
if exp[:name] == export_type
#export_options[:include] =
#render text: @entries_list.to_json
end
end
if params[:type]
case params[:type]
when "json"
when "xml"
render text: @entries_list.to_xml
end
else
respond_to do |format|
format.json { render json: @entries_list }
format.json { render xml: @entries_list }
end
end
end
end
module ImportActions
def import
import_type = params[:type]
end
end
end
################################################################################################################
######################################### Ajax Modules ###############################################
################################################################################################################
module Ajax
module InplaceEditActions
def edit_inplace
unless self.class.respond_to?(:get_kb_page)
render text: 'ERROR OCCURRED!!!'
return
end
local_page = self.class.get_kb_page(:update)
unless local_page
render text: 'ERROR OCCURRED!!!'
return
end
local_entry = self.entry_class.get_entry_by_id(params[:id], nil, {logger: string_logger})
if local_entry
result_string = ''
begin
unless local_entry[params[:field_name]] == params[:value]
local_entry[params[:field_name]] = params[:value]
local_entry.updated_at = Time.now
local_entry.updater = @current_user
unless local_entry.save
result_string = 'FAILED TO SAVE!!!'
end
end
rescue
result_string = 'ERROR OCCURRED!!!'
end
if result_string.blank?
begin
result_string = local_entry.send(params[:field_name])
rescue
result_string = 'ERROR OCCURRED!!!'
end
end
render text: result_string
else
render text: 'NOT FOUND!!!'
end
end
end
end
end
end
end