- 
      
- 
        Save Spaceghost/5456276 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
    
  
  
    
  | # 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 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
    
  
  
    
  | # Model | |
| # ----- | |
| class Visit < ActiveRecord::Base | |
| include ProtectedFields | |
| protected_attributes :visit_date, :name | |
| end | 
  
    
      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
    
  
  
    
  | # 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