Datts Right

Dynamic Attributes Done Right

Well, maybe it’s done right. This gem is more of an experiment at the moment. I also did this to learn more about metaprogramming.

I would like input from you guys. Is this something that is too messy?

Why make this?

  1. I needed to allow users to create their own dynamic attributes on certain records. I was already running PostgreSQL. My database and code was designed largely on relational structures. I was thinking of moving to MongoDB but saw that I’d pretty much have to rewrite all the models. Creating this gem was the first step to using dynamic attributes on my app.
  2. The available plugins out there that did something like this stuffed the dynamic attributes in a column in the model that had dynamic attributes. Because of this, you:
    • Could not order things by dynamic columns straight with SQL
    • Could not (as far as I know) chain scopes, like so: MyModel.where(:name_which_is_a_real_attribute => "Joe").where_dynamic_attribute(:phone_which_is_dynamic => "23218793")
    • Could not find by dynamic attribute straight with SQL, like so: MyModel.find_by_dynamic_attribute_phone_which_is_dynamic("2398291308")
    • Had to add migrations to each model that you wanted to have dynamic attributes

Contributing to dynamic_attributes_right

There are definitely things that don’t work, and could be done better. For example, it would be nice to:

  1. Do away with the special scopes, such as where_dynamic_attribute and order_by_dynamic_attribute. I’ve tried to override ActiveRecord to make the normal where and order methods to see if the attributes being passed were dynamic, and do the changes necessary to the resulting SQL, but my head started to hurt.
  2. Have smarter caching, so that finds with dynamic attributes aren’t very expensive.
  3. Have configurable “dynamic_attributes” table

Warning: the code is full of comments because I needed to visualize the code running.

If you want to contribute:

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright © 2011 Ramon Tayag. See LICENSE.txt for further details.