-
-
Save rthbound/5448639 to your computer and use it in GitHub Desktop.
rearranging
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 | |
# ------ | |
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 |
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
# Model | |
# ----- | |
class Visit < ActiveRecord::Base | |
include ProtectedFields | |
protected_attributes :visit_date, :name | |
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
# Controller | |
# ---------- | |
def update | |
@visit = Visit.find(params[:id]).protect_from(current_user) | |
if @visit.update_attributes(params[:visit]) | |
# Success | |
else | |
# Failure | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i'm in favor of allowing users to
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.