Created
March 5, 2012 15:22
-
-
Save AlSquire/1978727 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# * I think some attr should be filtered by controllers, | |
# while others by models (ie. computed values, as counter_cache) | |
# * Casting http params should not be the model direct responsibility ("1" => true, urg) | |
# * Using multiple controllers for a same model would be more convenient | |
# * I would love controllers to be more declarative | |
class Comment < ActiveRecord::Base | |
belongs_to :user # user_id (integer) | |
belongs_to :post # post_id (integer) | |
# title (string) | |
# content (text) | |
# created_at, updated_at (datetime) | |
end | |
class CommentsController < ApplicationController | |
# It's a "sugar" example, the same should be attainable with a inheriting class to be more | |
# specialised and/or modular | |
params_for :comment, allow: [:title, :content] do |p| | |
p.title { |value| value.strip } # I prefer stripping in the model, but this is for demonstration | |
strip :title, :content # Declarative alternative, custom methods for common patterns | |
p.post_id { params[:post_id] } # Because in a nested resource | |
end | |
def create | |
# Default behaviour is the same, so it can be DRYed easily (responders) | |
@comment = current_user.comments.create(params[:comment]) | |
... | |
# Or maybe use a special method | |
@comment = current_user.comments.create(params_for(:comment)) | |
end | |
end | |
# Need to think through: | |
# * Mass assignments over relations | |
# * Specialisation depending on actions and/or roles | |
# * Inheritance behaviour | |
# * Also share some validations controller side? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment