Skip to content

Instantly share code, notes, and snippets.

@rthbound
Forked from jschniper/protected_attributes.rb
Last active December 16, 2015 14:28
Show Gist options
  • Save rthbound/5448639 to your computer and use it in GitHub Desktop.
Save rthbound/5448639 to your computer and use it in GitHub Desktop.
rearranging
# 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
# Model
# -----
class Visit < ActiveRecord::Base
include ProtectedFields
protected_attributes :visit_date, :name
end
# Controller
# ----------
def update
@visit = Visit.find(params[:id]).protect_from(current_user)
if @visit.update_attributes(params[:visit])
# Success
else
# Failure
end
end
@rthbound
Copy link
Author

i'm in favor of allowing users to

include ProtectedRecord::DirtyModel
protected_fields :do_not_resuscitate, :organ_donor

Thus eliminating need for the :protected_keys argument when ProtectedRecord::UseCase::Update is initialized.

That said, users should still be able to inject :protected_keys and forgo including ProtectedRecord::DirtyModel in their models.

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