Last active
December 16, 2015 14:28
-
-
Save jschniper/5448529 to your computer and use it in GitHub Desktop.
An Alternative Interface
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
# Controller | |
# ---------- | |
def update | |
@visit = Visit.find(params[:id]).protect_from(current_user) | |
if @visit.update_attributes(params[:visit]) | |
# Success | |
else | |
# Failure | |
end | |
end | |
# Model | |
# ----- | |
class Visit < ActiveRecord::Base | |
include ProtectedFields | |
protected_attributes :visit_date, :name | |
end | |
# Module | |
# ------ | |
module ProtectedFields | |
# There is probably a nicer way to do this... | |
def self.included(base) | |
base.class_eval { | |
def self.protected_attributes(*attrs) | |
@@protected_attributes = attrs | |
end | |
} | |
end | |
def protect_from(user) | |
@protected = true | |
@user = user | |
self | |
end | |
before_save do | |
if @protected | |
# check fields against user | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment