github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

jcoglan / attr_locked

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 4
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Allows you to stop ActiveRecord fields changing after an object is first created — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Fixing a bug on FormHelper#datetime_select  produced by Rails 2.1 update 
(now the method accepts 2 parameteres instead of only one 
Roman Gonzalez (author)
Fri Aug 01 11:41:17 -0700 2008
commit  3ff58a75f6a064f918aa0d2f6a090283425b23bb
tree    602974d598dbe90791c3318bb87ca4ab4af056dc
parent  f34b8bb623e09201c3855d9175b97f057be8c789
attr_locked /
name age
history
message
file README Tue Jun 12 08:33:39 -0700 2007 Updated readme to explain associations. Manual ... [jcoglan]
file Rakefile Fri Jun 08 03:05:55 -0700 2007 Initial import - new plugin. [jcoglan]
file init.rb Fri Jun 08 09:54:13 -0700 2007 Added InstanceTag modifications and README. Now... [jcoglan]
file install.rb Fri Jun 08 03:05:55 -0700 2007 Initial import - new plugin. [jcoglan]
directory lib/ Fri Aug 01 11:41:17 -0700 2008 Fixing a bug on FormHelper#datetime_select pro... [Roman Gonzalez]
directory tasks/ Sun Jun 10 13:41:15 -0700 2007 Added test suite (requires plugging in to an ac... [jcoglan]
directory test/ Mon Jun 11 05:34:49 -0700 2007 Added ability to lock tables from saving, updat... [jcoglan]
file uninstall.rb Fri Jun 08 03:05:55 -0700 2007 Initial import - new plugin. [jcoglan]
README
== AttrLocked

+AttrLocked+ is a little Rails plugin that provides extra security for your +ActiveRecord+ models. It lets you specify, 
at the model level, that certain model attributes should never be allowed to change. Perfect for usernames or financial 
record-keeping.


=== In the model

Let's take an example: your application has users, who each have a username. Once created, that username should never 
change. +AttrLocked+ lets you specify this with a single line:

  class User < ActiveRecord::Base
    attr_locked :username
  end

Now, when you create a new +User+, you can of course assign it a username. But once the record has been saved 
(specifically, when <tt>new_record?</tt> returns false) you cannot reassign it. This is best illustrated with a little 
code:

  foo = User.new(:username => 'Bob')
  foo.save
  
  bob = User.find_by_username('Bob')
  bob.username
  #=> "Bob"
  bob.username = 'Mike'
  #=> "Mike"
  bob.username
  #=> "Bob"
  
  bob.update_attribute(:username, 'Freddie')
  #=> false
  bob.username
  #=> "Bob"

So, you are unable to assign using <tt>username=</tt> (or indeed using <tt>attributes=</tt>). +save+, 
+update_attributes+ and <tt>attributes=</tt> will silently ignore the locked attributes, just as they do for 
+attr_protected+. +update_attribute+ will return +false+ if you use it with a locked attribute.


=== In the view

To make sure people don't waste their time filling out forms, any form input that's linked to a locked attribute will be 
disabled automatically, unless you're creating a new record. So, you don't need to go around putting <tt>:disabled => 
!object.new_record?</tt> everywhere. Helpful, no?


=== Locking whole tables

You might have some database tables that are read-only. For example, there might be a table that stores a set of 
third-party geographic data that's used by your application. You can safeguard such tables against changes by using:

  class Location < ActiveRecord::Base
    table_locked
  end

With that in place, you cannot modify the properties of any +Location+ instances, even new ones not yet saved to the 
database. You cannot save, create, update or destroy (or +delete_all+) any records from the table.


=== Associations

You needn't worry that locking records or tables means they cannot be associated with anything. Using our geographic 
data example, we could have:

  # This table is for reference only
  class Town < ActiveRecord::Base
    table_locked
    has_many :users
  end
  
  class User < ActiveRecord::Base
    attr_locked :username
    belongs_to :town
  end

With this setup, we can still modify associations as usual:

  town = Town.find(:first)
  user = User.find(:first)
  
  # Saves immediately
  town.users = [user]
  
  # As does this
  town.users << user
  
  # This works too
  user.town = town
  user.save


=== License

Copyright (c) 2007 James Coglan

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server