Skip to content

Instantly share code, notes, and snippets.

@sj26
Created June 23, 2013 04:58
Show Gist options
  • Save sj26/5843855 to your computer and use it in GitHub Desktop.
Save sj26/5843855 to your computer and use it in GitHub Desktop.
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.
class AppleRecord < Record
end
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
class OrangeRecord < Record
end
class Record < ActiveRecord::Base
extend InheritenceBaseNaming
def [] type
descendents.find { |descendent| descendent.name == type }
end
end
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
@cableray
Copy link

It's Inheritance, right, not Inheritence?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment