Repository is archived
No commit activity in last 3 years
No release in over 3 years
CustomAttributes allows you to add custom attributes to ActiveRecord objects, optionally scoped by another model (e.g. users).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

~> 3
 Project Readme

CustomAttributes

Introduction

CustomAttributes allows you to add custom attributes to ActiveRecord objects, optionally scoped by another model (e.g. users or accounts).

Installation

To install, simply run:

$ gem install custom_attributes

Or put this in your Gemfile:

gem 'custom_attributes'

To create the migration for the custom_attributes tables, run:

rails g custom_attributes:install

Usage

Define that a model has custom attributes:

class Contact < ActiveRecord::Base
  has_custom_attributes
end

Now, add custom attributes to the model:

Contact.add_custom_attribute :internal_id
Contact.add_custom_attribute "internal_id"
Contact.add_custom_attribute [:internal_id, :nickname]

Use the new custom attributes like normal ones:

c = Contact.new
c.internal_id = "ABC-123"
c.nickname = "Dude"
c.save

Add scope by defining which controller method returns it:

::CustomAttributes.scope_method = :current_user

Or set the scope object directly:

::CustomAttributes.scope = User.first

Or override the controller method that returns the scope:

class ApplicationController < ActionController::Base
  def custom_attribute_scope
    # scope determination
    if current_user.can?(:customize, resource)
      current_user
    end
  end
end

Inspiration

has-magic-columns: http://code.google.com/p/has-magic-columns/

License

Copyright (c) 2011 René Föhring, released under the MIT license