Skip to content
Newer
Older
100644 66 lines (42 sloc) 1.92 KB
5ffc073 @cjohansen Initial git commit
authored Nov 25, 2008
1 = acts_as_prototype
2
3 Acts as prototype allows any ActiveRecord based model object to be annotated
4 with arbitrary properties. The object is given a prototype object, and through
5 this it can be a part of a prototype chain, allowing a cascading property
6 system which works much like JavaScripts objects and properties.
7
8 == Installation
9
8f96dc3 @cjohansen Updated install instructions
authored Nov 25, 2008
10 ruby script/plugin install git://github.com/cjohansen/acts_as_prototype.git
5ffc073 @cjohansen Initial git commit
authored Nov 25, 2008
11
12 === Setup database
13
14 ruby script/generate property_migration
15 rake db:migrate
16
17 == Usage
18
19 Properties can be set and read from the global scope like a normal model:
20
21 prop = Property.set("logo_url", "/images/rails.png")
22 prop.save
23
24 Any object that should be configurable with properties should simply do this:
25
26 class Account < ActiveRecord::Base
27 acts_as_prototype
28 end
29
30 Now you can get properties through an instance of MyModel:
31
32 account = Account.find 1
33 account.properties[:logo_url] #=> "/images/rails.png"
34
35 Override settings:
36
37 account.properties[:logo_url] = "/images/my_logo.png"
38 account.save # Causes properties to be saved also
39
40 Set new properties for single objects (ActiveRecord objects are stored by
41 reference):
42
43 account.properties[:profile] = Profile.find(2)
44
45 You can generate new ActiveRecord objects which share properties with another
46 object:
47
48 class Project < ActiveRecord::Base
49 acts_as_prototype
50 end
51
52 project = account.beget(Project, { :name => "New project" })
53 project #=> New project object which shares properties with account
54
55 project.properties[:logo_url] = "/images/my_project.png"
56 account.properties[:logo_url] #=> "/images/my_logo.png"
57
58 Updating the original objects properties will reflect on the begotten objects
59 as long as they haven't overridden properties:
60
61 project.properties[:profile] #=> #<Profile id: 2...>
62 account.properties[:profile] = Profile.find(3)
63 project.properties[:profile] #=> #<Profile id: 3...>
64
65 Copyright (c) 2008 Christian Johansen, released under the MIT license
Something went wrong with that request. Please try again.