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.
- #default_serializer_options ⇒ Object
-
#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.
-
#serialize(stuff) ⇒ Object
nasty hack until I can get an answer on the official way to remove the instance root keys in a list.
-
#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
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 144 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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 59 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 |
#default_serializer_options ⇒ Object
225 226 227 228 229 230 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 225 def { root: get_model.model_name.route_key#, # model_class: get_model } end |
#destroy ⇒ Object
DELETE /table_name/1 DELETE /table_name/1.json
99 100 101 102 103 104 105 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 99 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
75 76 77 78 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 75 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
170 171 172 173 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 170 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
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 176 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
107 108 109 110 111 112 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 107 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
28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 28 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 body:@instances.to_json, content_type:'application/json'} # using the json parameter nests objects inside of quick_dry keys format.json { render json:serialize(@instances)}#, each_serializer: QuickDrySerializer}# serializer:QuickDryArraySerializer} format.html { render 'quick_dry/index'} end end |
#instance_params ⇒ Object
Never trust parameters from the scary internet, only allow the white list through.
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 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 115 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 pascal = model.model_name.singular_route_key.camelize camel = model.model_name.singular_route_key.camelize(:lower) snake = model.model_name.singular_route_key # instance_name if body.has_key? snake params.merge!(body) # instanceName elsif body.has_key? camel params.merge!({snake => body[camel]}) # InstanceName elsif body.has_key? pascal params.merge!({snake => body[pascal]}) else params[model.model_name.singular_route_key] = body end 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
162 163 164 165 166 167 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 162 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
41 42 43 44 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 41 def new @instance = get_model.new render 'quick_dry/new' end |
#serialize(stuff) ⇒ Object
nasty hack until I can get an answer on the official way to remove the instance root keys in a list
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 10 def serialize stuff if stuff.is_a? Array or stuff.is_a? ActiveRecord::Relation json = render_to_string json:QuickDryArraySerializer.new(stuff, root:get_model.model_name.route_key ) hash = JSON.parse(json) temp = [] if hash[get_model.model_name.route_key].first.has_key? get_model.model_name.route_key hash[get_model.model_name.route_key].each{|x| temp << x[get_model.model_name.route_key]} hash[get_model.model_name.route_key] = temp return hash.to_json end return json elsif stuff.is_a? get_model end end |
#show ⇒ Object
GET /table_name/1 GET /table_name/1.json
48 49 50 51 52 53 54 55 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 48 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/controllers/quick_dry/quick_dry_controller.rb', line 82 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 |