This is an ActiveRecord act I've been implementing for a webshop system I'm working on. I thought it would be nice to get it integrated into Rails as I think others may find it useful also.
Specify this act to make the table act as a vertical table.
This act use these columns. You can override column names by setting :var_column and :data_column
- var TEXT NOT NULL -- The variable name. Should be indexed.
- data BLOB -- The variable data.
- updated_at TIMESTAMP -- Optional. May also be called <tt>updated_on</tt>.
These methods will be added:
- get_with_attributes(var) -- Return the row as a normal AR object with all columns.
- set_with_attributes(var,data,attributes={}) -- Set the variable with data for additional db-columns.
- to_h(include_defaults=false) -- Return a hash of all var => data
- all_records(include_defaults=false) -- An array of all variable records
- delete(vars) -- Delete one or more variables.
- clear -- Delete all variables.
- updated_on/at -- The time a variable was last updated if you have a updated_on/at column.
Example:
class Settings < ActiveRecord::Base
acts_as_vertical
end
Usage example:
@settings = Settings.new :conditions => {:user_id => 1, :shop_id => 2},
:preload => [:bgcolour, :textcolour],
:defaults => {:bgcolour => 'white'}
@settings.bgcolour # => 'white' (unless bgcolour was previously set)
@settings.textcolour # => nil (unless textcolour was previously set)
@settings.bgcolour = "#f4f4ff" # Will insert or update: var = 'bgcolour', data = '#f4f4ff', user_id = 1, shop_id = 2
@settings.textcolour = "black" # Will insert or update: var = 'textcolour', data = 'black', user_id = 1, shop_id = 2
@settings.bgcolour # => '#f4f4ff'
@settings.textcolour # => 'black'
Configuration options for acts_as_vertical (class-method) are:
- var_column - specifies the column name to use for variable names (default: var).
- data_column - specifies the column name to use for variable data (default: data).
- preload - set to true if you want to preload all variables by default.
- autosave - set to true to enable autosave by default.
Configuration for ModelName.new are:
- preload - an array with variable names or true (for all) for variables to preload.
- autosave - true if you want variables to be saved immediatly when they are set (no need to call .save).
- conditions - hash with conditions for extra columns. For example {:user_id => 5}
- defaults - hash with default values for variables.
I personally use it like this (among other things):
class Settings < ActiveRecord::Base
acts_as_vertical :preload => true, :autosave => true
end
class LookSettings < Settings
end
class LookDefaults < Settings
end
class StylesheetsController < ApplicationController
def render_stylesheet
@headers['Content-Type'] = 'text/css'
@settings = LookSettings.new :defaults => LookDefaults.new.to_h
render_action params[:id].split('.').first
end
end
app/views/stylesheets/main.rhtml:
body {
background: <%= @settings.background %>
color: <%= @settings.textcolor %>
}
(I also page-cache the rendered css and have a SettingsObserver that deletes the cached css-files when settings are changed)
I tried to follow all Rails coding conventions, using 2 spaces instead of tabs etc... The version that is attached here is a self-contained file that you can put into lib/, require it and you are ready to use it.
I've also made lots of unit tests for it.