Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #1883 (closed enhancement: wontfix)

Opened 4 years ago

Last modified 2 years ago

[RESEARCH] acts_as_vertical

Reported by: Per Wigren <tuxie@dekadance.se> Assigned to: David
Priority: normal Milestone:
Component: ActiveRecord Version: 0.13.1
Severity: normal Keywords:
Cc: fbeausoleil@ftml.net

Description

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.

Attachments

acts_as_vertical.rb (12.7 kB) - added by tuxie@dekadance.se on 08/01/05 05:01:15.
lib/acts_as_vertical.rb
acts_as_vertical_test.rb (3.3 kB) - added by tuxie@dekadance.se on 08/01/05 05:03:50.
test/unit/acts_as_vertical_test.rb

Change History

08/01/05 05:01:15 changed by tuxie@dekadance.se

  • attachment acts_as_vertical.rb added.

lib/acts_as_vertical.rb

08/01/05 05:03:50 changed by tuxie@dekadance.se

  • attachment acts_as_vertical_test.rb added.

test/unit/acts_as_vertical_test.rb

08/03/05 03:17:39 changed by Mark van Eijk

  • summary changed from PATCH: acts_as_vertical to [PATCH] acts_as_vertical.

08/19/05 14:16:03 changed by anonymous

  • cc set to fbeausoleil@ftml.net.

09/03/05 08:18:04 changed by htonl

  • summary changed from [PATCH] acts_as_vertical to [RESEARCH] acts_as_vertical.

Thank you for submitting this. I'm moving this from [PATCH] to [RESEARCH]. While I think it's a neat idea, it's not a patch per se, it's just a couple Ruby files. If you could re-work this as a proper diff and attach that, we can further evaluate it as an act.

I have looked over the code and I like it, except I would like for you to work around adding a public wrapper for construct_finder_sql. If you really need to get around its protection, you can use #send, or demonstrate a good reason for it to be public.

11/24/05 04:46:32 changed by anonymous

  • type changed from defect to enhancement.

06/03/06 14:37:40 changed by brad@madriska.com

This is a really neat idea -- probably better off as a plugin though. I guarantee many more people would use this (including me) if it were available from that page and could be installed through http or svn.

05/22/07 19:45:12 changed by josh

  • status changed from new to closed.
  • resolution set to wontfix.

Would be better off as a plugin.