Fork me on GitHub

Qcontent

What

Qcontent is a growing collection of ruby modules and mix-ins for extending ActiveRecord. The mix-ins address a variety of common problems/features needed in Content Management Systems (CMS).

Why

Over the past number of years of developing Ecommerce and CMS apps I found there was a lot of code that I was sharing between apps. The goal here was to extract the ActiveRecord extensions into shared modules, mix-ins, and macros so that they can be managed and maintained in a single place. These modules also go hand-in-hand with the ActionController/View extensions found in Qadmin.

Usage

Each module and mix-in has a different use which I’ll describe below, but the general idea is once installed in your Rails app, Qcontent modules can be included like:

class Article < ActiveRecord::Base
  include Qcontent::Published
  include Qcontent::Assets
  
  #...
end

Qcontent::Published

The Published module gives a number of common methods for determining the published state of content in a CMS.

If you have a published_at column on your ActiveRecord class you automatically get:

# Article has a published_at :datetime column

Article.published 
# scope that only returns articles where published at is in the past

Article.recent(3) 
# returns the 3 most recent records ordered by published date

Article.published? 
# published at is in the past

Article.published_only { #... } 
# with_scope

Event without a published_at column this can still be used. You just have to define a class method active_conditions. For example:

class Item < ActiveRecord::Base
  include Qcontent::Published
  # Item has no published_at but does have a boolean @active@ column
  
  def self.active_conditions
    {:active => true}
  end
end

# Published gives you:
Item.active_only 
# named scope that uses the active conditions

Item.only_active {#...}
# with_scope that can be used in a @around_filter@ in ActionController

Qcontent::Dimension

Dimension is a common class for extracting image/asset dimensions.

It can take a variety of inputs.

# these all return equivilent Dimension instances
Qcontent::Dimension.new('100x100')
Qcontent::Dimension.new([100, 100])
Qcontent::Dimension.new({:width => 100, :height => 100})

Qcontent::Pricing

Pricing extract the pattern of having Money objects stored on a model as an integer.

class Item < ActiveRecord::Base
  include Qcontent::Pricing
  
  # has column @retail_price_cents@ :integer
  has_price :retail_price, :default => nil
  
  # has column @wholesale@ :integer
  has_price :wholesale_price, :attribute => :wholesale
end

# this gives you automatic price conversions 
item = Item.first
#=> <Item>
item.retail_price = "$40"
item.retail_price
#=> <Money @cents=4000>

item.retail_price = 40
item.retail_price
#=> <Money @cents=40>

item.retail_price = '40.23'
item.retail_price
#=> <Money @cents=4023>

More

There a bunch more mix-ins coming soon. Stay tuned! Watch the project on github for the latest updates.

Dependencies

  • rubygems >= 1.3.1
  • activesupport >= 2.2.0
  • activerecord >= 2.2.0
  • money >= 2.0

Installing

sudo gem install qcontent
        

Or directly from github:

sudo gem install quirkey-qcontent -s http://gems.github.com
        

Github in thier infinite awesomeness also lets you download the source in multiple formats.

Bugs/Feature Requests

The full and most up to date source is at github:

http://github.com/quirkey/qcontent/

You can clone or fork from there to fix or break the code.

I’m happy to take feature requests/bug reports. Just email me (contact below) or message me on github.

License

This code is free to use under the terms of the MIT license.

Contact

Feel free to email me at aaron at quirkey dot com.

Please check out my blog.

If you like or use this library – I don’t want donations – but you can recommend me on workingwithrails.com or hire me to work on your next project.