Skip to content

Instantly share code, notes, and snippets.

@jschniper
Last active December 16, 2015 14:28
Show Gist options
  • Save jschniper/5448529 to your computer and use it in GitHub Desktop.
Save jschniper/5448529 to your computer and use it in GitHub Desktop.
An Alternative Interface
# 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