-
-
Save cableray/bd36b7e1c49a11661c7c to your computer and use it in GitHub Desktop.
How to do STI without separate controller per child: 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AppleRecord < Record | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module InheritanceBaseNaming | |
class Name < ActiveModel::Name | |
delegate :param_key, :singular_route_key, :route_key, to: :base_name | |
def base_name | |
@base_name = @klass.base_class.model_name | |
end | |
end | |
def model_name | |
Name.new(self) | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrangeRecord < Record | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Record < ActiveRecord::Base | |
extend InheritenceBaseNaming | |
def [](type) | |
descendents.find { |descendent| descendent.name == type } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This fixes: