Plugins - nubs
Add to favoritesNubs
Nubs are little nuggets of information that have little to no validation requirements. Nubs are meant to be attached to models and accessed similar to how you would access values of hash (via key).
Nubs are great for storing things like:
- A user's non-vital profile information (bio, favorite books, etc.)
- Configuration parameters for some component
- I am certain there are more
Nubs are only meant to be strings. While keys are case sensitive, they have indifferent access; meaning, you can use symbols or strings interchangeably.
Example
class User < ActiveRecord::Base
nubs :profile
end
user = User.find(:first)
user.profile[:bio] = "Fudge monkey"
user.profile[:bio]
=> "Fudge monkey"
user.profile[:new_attribute]
=> nil
user.profile[:new_attribute] = "Funky beans"
user.profile[:new_attribute]
=> "Funky beans"
Details
When you read a nub that has no corresponding record, Nubs will always return nil. When writing a value for a nub, Nubs will figure out whether to create or save the record.
Saving is done immediately. You do not need to save the parent record. This was a choice I intentionally made since this is how I wanted to use it. Nubs are not meant to be validation heavy since you will likely be storing all types of info. Some may say, "That's silly!". I will say, "That's handy!".
A Nub is itself an ActiveRecord model. In the example above, there will need to be a Profile model that has at least three attributes: :userid (for a belongsto association), a :key, and a :value. If need be, you can provide all of the same options to the nubs</code> macro as you would provide to a <code>has_many; except for the :extend option which is required explicitly by the Nubs plugin.
For instance, you could use a different foreign key column like so:
class User < ActiveRecord::Base
nubs :profile, :foreign_key => :some_other_id
end
The above examples give access to the value stored in the database for the given key. If, however, you want direct access to the Nub record via its key, you can use the of method. For example:
user = User.find(:first)
user.profile[:bio] = "Fudge monkey"
user.profile.of(:bio)
=> #<Profile id: 1, user_id: 1, key: "bio", value: "Fudge monkey" ...>
Installing
./script/plugin install git://github.com/jaknowlden/nubs.git
Using
For the above example, you would use a generator in the following way:
$RAILS_ROOT/script/generate nubs User Profile
This will create a migration for Profile, create the ActiveRecord model, and create a unit test. You will then need to modify the User model and add the following:
class User < ActiveRecord::Base
nubs :profile
# ...
end
Nesting Nubs
Though not explicitly supported, you could theoretically support a fixed number of nested Nubs.
WARNING: the following is completely untested. You might do something like:
$RAILS_ROOT/script/generate nubs User Profile
$RAILS_ROOT/script/generate nubs Profile SubProfile
rake db:migrate
Then you would modify your models like so:
class User < ActiveRecord::Base
nubs :profile
end
class Profile < ActiveRecord::Base
belongs_to :profile
nubs :sub_profile
end
class SubProfile < ActiveRecord::Base
belongs_to :profile
end
You could then access nubs in the following ways:
user = User.find(:first)
user.profile[:bio] = "Foo"
user.profile.of(:bio)[:mass] = "Bar"
Todo
- Support something that looks like nesting via a naming convention for the key
- Allow users to provide their own :extend option to
nubs - Provide a nice shortcut for actually deleting nub records. Currently, you can only set them to nil
Legal
Copyright (c) 2008 Justin Knowlden, released under the MIT license
