Class: QuickDry::QuickDryController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- QuickDry::QuickDryController
- Defined in:
- app/controllers/quick_dry/quick_dry_controller.rb
Instance Method Summary collapse
- #append_route(proc: nil, &block) ⇒ Object (also: #append_routes)
-
#create ⇒ Object
POST /table_name POST /table_name.json.
-
#destroy ⇒ Object
DELETE /table_name/1 DELETE /table_name/1.json.
-
#edit ⇒ Object
GET /table_name/1/edit.
-
#get_model ⇒ Object
this will only work in conjunction with the routing provided with the engine.
-
#get_paged_search_results(params, user: nil, model: nil) ⇒ Object
Assumes the existance of a User model.
- #get_url(target: @instance) ⇒ Object
-
#index ⇒ Object
GET /table_name GET /table_name.json.
-
#instance_params ⇒ Object
Never trust parameters from the scary internet, only allow the white list through.
- #instantiate_paths ⇒ Object
-
#new ⇒ Object
GET /table_name/new.
-
#show ⇒ Object
GET /table_name/1 GET /table_name/1.json.
-
#update ⇒ Object
PATCH/PUT /table_name/1 PATCH/PUT /table_name/1.json.
Instance Method Details
#append_route(proc: nil, &block) ⇒ Object Also known as: append_routes
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 114 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 |
#create ⇒ Object
POST /table_name POST /table_name.json
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 41 def create @instance = get_model.new(instance_params) respond_to do |format| if @instance.save flash[:notice] = "#{get_model.name} was successfully created." format.html { render 'quick_dry/show' } format.json { render json:@instance, status: :created, location: get_url } else format.html { render 'quick_dry/new' } format.json { render json:@instance.errors, status: :unprocessable_entity } end end end |
#destroy ⇒ Object
DELETE /table_name/1 DELETE /table_name/1.json
81 82 83 84 85 86 87 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 81 def destroy get_model.destroy(params[:id]) respond_to do |format| format.html { redirect_to "/#{get_model.model_name.route_key}", notice: "#{get_model.name} was successfully destroyed." } format.json { head :no_content } end end |
#edit ⇒ Object
GET /table_name/1/edit
57 58 59 60 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 57 def edit @instance = get_model.find(params[:id]) render 'quick_dry/edit' end |
#get_model ⇒ Object
this will only work in conjunction with the routing provided with the engine
140 141 142 143 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 140 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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 146 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 |
#get_url(target: @instance) ⇒ Object
89 90 91 92 93 94 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 89 def get_url target:@instance target_url = "/unknown_route" return target_url = "#{get_model.model_name.route_key}" if target.is_a? ActiveRecord::Relation return target_url = "#{get_model.model_name.route_key}/#{target.id}" if target.is_a? ActiveRecord::Base return target_url end |
#index ⇒ Object
GET /table_name GET /table_name.json
11 12 13 14 15 16 17 18 19 20 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 11 def index # results = get_paged_search_results(params,user:current_user) # params = results[:params] @instances = get_model.all # render 'quick_dry/index' respond_to do |format| format.json { render json:@instances} format.html { render 'quick_dry/index'} end end |
#instance_params ⇒ Object
Never trust parameters from the scary internet, only allow the white list through.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 97 def instance_params model = get_model # get all params except for id, and the standard dates respond_to do |format| format.html { } format.json do body = JSON.parse(request.body.read) if body.is_a? Hash and body.has_key? model.model_name.singular_route_key params.merge!(body) elsif body.is_a? Hash params[model.model_name.singular_route_key] = body end end end return 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_paths ⇒ Object
132 133 134 135 136 137 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 132 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 |
#new ⇒ Object
GET /table_name/new
23 24 25 26 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 23 def new @instance = get_model.new render 'quick_dry/new' end |
#show ⇒ Object
GET /table_name/1 GET /table_name/1.json
30 31 32 33 34 35 36 37 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 30 def show @instance = get_model.find(params[:id]) respond_to do |format| format.json { render json:@instance} format.html { render 'quick_dry/show'} end end |
#update ⇒ Object
PATCH/PUT /table_name/1 PATCH/PUT /table_name/1.json
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 64 def update @instance = get_model.find(params[:id]) respond_to do |format| if @instance.update(instance_params) flash[:notice] = "#{get_model.name} was successfully updated." format.html { render 'quick_dry/show' } format.json { render json:@instance, status: :ok, location: get_url} else format.html { render 'quick_dry/edit' } format.json { render json: @instance.errors, status: :unprocessable_entity} end end end |