Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

How I do STI: Utilize descendent tracking and override model_name so they use the same URL helpers and parameters as their base class. Makes things like responders and form_for work as expected, while preserving things like to_partial_path.

View apple_record.rb
1 2 3 4 5 6 7 8 9 10 11 12
module InheritenceBaseNaming
def model_name
@_model_name ||= super.tap do |name|
unless self == base_class
the_base_class = base_class
%w(param_key singular_route_key route_key).each do |key|
name.singleton_class.send(:define_method, key) { the_base_class.model_name.public_send(key) }
end
end
end
end
end
View apple_record.rb
1 2 3 4 5 6 7
class Record < ActiveRecord::Base
extend InheritenceBaseNaming
 
def [] type
descendents.find { |descendent| descendent.name == type }
end
end
View apple_record.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
class RecordsController < ApplicationController
before_filter :prepare_records
before_filter :prepare_record, only: [:show, :edit, :update, :destroy]
 
respond_to :html, :json
 
def new
respond_with @record = Record[params.require(:type)].new
end
 
def create
respond_with @record = Record[params.require(:record_type)].create(record_params)
end
 
def show
respond_with @record
end
 
def edit
respond_with @record
end
 
def update
@record.update_attributes(record_params)
respond_with @record
end
 
def destroy
@record.destroy
respond_with @record
end
 
private
 
def prepare_records
@records = Record.scoped
end
 
def prepare_record
@record = @records.find(params[:id])
end
 
def record_params
params.require(:record).permit(:name, :content)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.