Class: QuickDry::QuickDryController

Inherits:
ApplicationController show all
Defined in:
app/controllers/quick_dry/quick_dry_controller.rb

Instance Method Summary collapse

Instance Method Details

#append_route(proc: nil, &block) ⇒ Object Also known as: append_routes



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 68

def append_route(proc:nil,&block)
	begin
		_routes = Rails.application.routes
		_routes.disable_clear_and_finalize = true
		_routes.clear!
		Rails.application.routes_reloader.paths.each{ |path| load(path) }

		_routes.draw &proc unless proc.blank?
		_routes.draw &block if block_given? and proc.blank?

		_routes.finalize!
	ensure
		_routes.disable_clear_and_finalize = false
	end
end

#createObject



27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 27

def create
	@instance = get_model.new(instance_params)

	if @instance.save
		flash[:notice] = 'Comment was successfully created.'
		render 'quick_dry/show'
	else
		render 'quick_dry/new'
	end
end

#destroyObject



55
56
57
58
59
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 55

def destroy
	@instance = get_model.find(params[:id]).destroy
	# flash[:notice] = "#{get_model.to_s} was successfully destroyed."
	redirect_to "/#{get_model.model_name.route_key}", notice: "#{get_model.to_s} was successfully destroyed."
end

#editObject



38
39
40
41
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 38

def edit
	@instance = get_model.find(params[:id])
	render 'quick_dry/edit'
end

#get_modelObject

this will only work in conjunction with the routing provided with the engine



94
95
96
97
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 94

def get_model
	model = request.params[:table_name].classify.constantize unless request.params[:table_name].blank?
	# model.blank? ? return nil : return model
end

#get_paged_search_results(params, user: nil, model: nil) ⇒ Object

Assumes the existance of a User model



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 100

def get_paged_search_results(params,user:nil,model:nil)
	params[:per_page] = 10 if params[:per_page].blank?
	params[:page] = 1 if params[:page].blank?
	# a ghetto user check, but for some reason a devise user is not a devise user...
	user = User.new if user.blank? or !user.class.to_s == User.to_s

	# get the model in question
	# there has got to be a better way to do this... I just can't find it
	# model = params[:controller].blank? ? self.class.name.gsub('Controller','').singularize.constantize : params[:controller].classify.constantize
	if model.blank? 
		model = request.params[:table_name].classify.constantize unless request.params[:table_name].blank?
		return nil if model.blank?
	end

	# initialize un-paged filtered result set
	result_set = model.none

	# create where clauses to filter result to just the customers the current user has access to
	customer_filter = ""
	user.customers.each do |cust|
		if model.column_names.include? "cust_id"
			customer_filter << "(cust_id = '#{cust.cust_id(true)}') OR " unless cust.cust_id.blank?
		elsif model.attribute_alias? "cust_id"
			customer_filter << "(#{model.attribute_alias "cust_id"} = '#{cust.cust_id(true)}') OR " unless cust.cust_id.blank?
		elsif model.column_names.include? "order_number"
			customer_filter << "(order_number like '#{cust.prefix}%') OR " unless cust.prefix.blank?
		elsif model.attribute_alias? "order_number"
			customer_filter << "(#{model.attribute_alias "order_number"} like '#{cust.prefix}%') OR " unless cust.prefix.blank?
		end
	end
	customer_filter << " (1=0)"

	# create where clauses for each search parameter
	if params[:columns].blank?
		result_set = model.where(customer_filter)
	else
		where_clause = ""
		params[:columns].each do |name, value|
			where_clause << "(#{model.table_name}.#{name} like '%#{value}%') AND " unless value.blank?
		end
		where_clause << " (1=1)"

		result_set = model.where(customer_filter).where(where_clause)
	end

	instances = model.paginate(page: params[:page], per_page: params[:per_page]).merge(result_set).order(updated_at: :desc)
	return {instances:instances,params:params}
end

#indexObject

GET /customer_orders GET /customer_orders.json



10
11
12
13
14
15
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 10

def index
	# results = get_paged_search_results(params,user:current_user)
	# params = results[:params]
	@instances = get_model.all
	render 'quick_dry/index'
end

#instance_paramsObject

Never trust parameters from the scary internet, only allow the white list through.



62
63
64
65
66
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 62

def instance_params
	model = get_model
	# get all params except for id, and the standard dates
	params.require(model.model_name.singular_route_key.to_sym).permit(model.attribute_names.collect{|x| x.to_sym} - [:id,:created_at,:updated_at])
end

#instantiate_pathsObject



86
87
88
89
90
91
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 86

def instantiate_paths
	route = eval %(lambda {resources :#{get_model.model_name.route_key}, controller: 'quick_dry'})
	append_route proc:route
	# append_route {resources get_model.model_name.route_key.to_sym, path: 'quick_dry'}
	# routes= Rails.application.routes.routes.map { |route| {alias: route.name, path: route.path.spec.to_s, method: "#{route.defaults[:controller]}##{route.defaults[:action]}"}}
end

#newObject



17
18
19
20
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 17

def new
	@instance = get_model.new
	render 'quick_dry/new'
end

#showObject



22
23
24
25
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 22

def show
	@instance = get_model.find(params[:id])
	render 'quick_dry/show'
end

#updateObject



43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 43

def update
	@instance = get_model.find(params[:id])

	if @instance.update(instance_params)
		flash[:notice] = "#{get_model.to_s} was successfully updated."
		render 'quick_dry/show'
	else
		# flash[:error] = '#{get_model.to_s} could not be updated.'
		render 'quick_dry/edit'
	end
end