Giter Site home page Giter Site logo

damianham / has_generic_list Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 136 KB

Ruby on Rails plugin to store lists of data associated with an active record model without having to create a table for each type of data. All data is stored in a generic table.

Home Page: http://github.com/damianham/has_generic_list

License: MIT License

Ruby 100.00%

has_generic_list's Introduction

has_generic_list

is a plugin to store lists of data associated with a model without having to create lots of tables, one for each type of data.

If you find this plugin useful, please consider a donation to show your support!

http://www.paypal.com/cgi-bin/webscr?cmd=_send-money

Email address: [email protected]

You can also consider hiring me to develop your web application, please drop me a line to discuss your project.

Installation

ruby script/plugin install git://github.com/damianham/has_generic_list.git

or add to the Gemfile with

gem 'has_generic_list', :git => 'git://github.com/damianham/has_generic_list.git'

Prepare database

Generate and apply the migration:

ruby script/generate has_generic_list_migration rake db:migrate

Basic usage

Let's suppose you have a Company model and you want to store lists of data associated with each Company e.g

  • countries of operation
  • languages spoken
  • product names...... etc. etc. you get the idea

i.e lists of any kind of data. You could also serialize an object and store that as the list item value as long as the serialized data length is less than the item_value column length in the generic_list_items table. If you want to store larger data then change the item_value column to a text column.

The first step is to add has_generic_list to the Company class:

class Company < ActiveRecord::Base
	  # the type names MUST be singular, if typename == typename.pluralize then an exception is raised
	  has_generic_list :types => [:country,:language,:office,:person] # as many types as you need
end

We can now use the pluralized methods generated by has_generic_list for each data type which work like regular array attribute accessors.

c = Company.find(:first)

c.countries # [] c.countries = ["UK", "US","CA"]

c.languages # [] c.languages = ["English", "French","Spanish"]

c.countries # ["UK", "US","CA"] c.languages # ["English", "French","Spanish"]

To display the value as a String use to_s, i.e.

c.languages[0].to_s # "English"

You can get all elements of the list as a comma separated String with to_s on the list itself, i.e

c.languages.to_s # "English, French, Spanish"

You can also add or remove elements of data.

c.languages.add("German", "Dutch") c.languages.remove("French")

You can do the normal array traversal on the list but you have to use the add/remove methods to modify the list.

Finding objects by the generic list data

To retrieve objects that have a particular list entry use _find_with_list, i.e

companies = Company.languages_find_with_item('Spanish')

which is just a convenience method for

Company.find_with_generic_list_item(:language,'Spanish') # note the singular type name

and to just get the object identifiers use

company_ids = Company.languages_find_ids_with_item('Spanish')

which returns an array of ids

Other

Problems, comments, and suggestions all welcome [email protected]

has_generic_list's People

Contributors

damianham avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.