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

matthewrudy / has_one_accessor

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 3
    • 0
  • Source
  • Commits
  • Network (0)
  • 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.

Access attributes on HasOne Associations as though they were on the base class — Read more

  cancel

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

This URL has Read+Write access

less reliant on Rails being initialized before we load the plugin 
Matthew Rudy Jacobs (author)
Sun Dec 07 07:40:59 -0800 2008
commit  2604e5992f3e4792635cfb447cc0245ef082f33e
tree    4f47698a89bece40f2db15ce190cfefc73d51f39
parent  d7f3bb806599c1eac4d002000f366af4a9c7b9c8
has_one_accessor /
name age
history
message
file MIT-LICENSE Loading commit data...
file README
file Rakefile
file init.rb
directory lib/
README
has_one_accessor
==========

A simple plugin to allow you to use an ActiveRecord HasOne Association's attributes like those of your own model.
It will give you nice setter and getter methods that will dynamically build the association, set it, and save it only on 
save of the underlying record.
This is useful for taking input from forms;
  User.new(params[:user]) is much nicer than
  
  auth_params = params[:user].delete(:auth)
  user = User.new(params[:user])
  user.authentication || user.build_authentication
  user.authentication.openid_url = auth_params[:openid_url]
  ...blah blah blah...

Simplicity and peace of mind is but a `has_one_accessor` away

Example
=======

  class User
    has_one :authentication
    has_one_accessor :authentication, [:openid_url, :facebook_token], :allow_blank => false
  end

this gives us

  user.authentication_openid_url
  user.authentication_openid_url=
  user.authentication_facebook_token
  user.authentication_facebook_token=

and with :allow_blank => false, it'll remove records with blank values on save.

also, if you prefer to generate methods without a :prefix

  has_one_accessor :authentication, :google_id, :prefix => false

will give us

  user.google_id
  user.google_id=

with all of the same niceties

Copyright (c) 2008 [Matthew Rudy Jacobs], released under the MIT license
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