meta_types 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +184 -0
- data/Rakefile +40 -0
- data/app/assets/javascripts/meta_types/application.js +15 -0
- data/app/assets/stylesheets/meta_types/application.css +13 -0
- data/app/controllers/meta_types/application_controller.rb +4 -0
- data/app/helpers/meta_types/application_helper.rb +4 -0
- data/app/models/meta_type.rb +36 -0
- data/app/models/meta_type_member.rb +29 -0
- data/app/models/meta_type_property.rb +74 -0
- data/app/views/layouts/meta_types/application.html.erb +14 -0
- data/config/routes.rb +2 -0
- data/db/migrate/20120507130230_create_hstore.rb +9 -0
- data/db/migrate/20120507130237_meta_type.rb +19 -0
- data/db/migrate/20120507130255_meta_type_property.rb +23 -0
- data/db/migrate/20120507130303_meta_type_member.rb +18 -0
- data/lib/meta_types.rb +9 -0
- data/lib/meta_types/active_record.rb +48 -0
- data/lib/meta_types/engine.rb +5 -0
- data/lib/meta_types/meta_properties.rb +93 -0
- data/lib/meta_types/meta_property.rb +27 -0
- data/lib/meta_types/meta_property_type.rb +59 -0
- data/lib/meta_types/version.rb +3 -0
- data/lib/tasks/meta_types_tasks.rake +4 -0
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/application.js +16 -0
- data/test/dummy/app/assets/stylesheets/application.scss +15 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/controllers/dashboard_controller.rb +3 -0
- data/test/dummy/app/controllers/meta_type_properties_controller.rb +7 -0
- data/test/dummy/app/controllers/meta_types_controller.rb +7 -0
- data/test/dummy/app/controllers/things_controller.rb +34 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/thing.rb +6 -0
- data/test/dummy/app/views/dashboard/index.html.erb +14 -0
- data/test/dummy/app/views/layouts/application.html.erb +58 -0
- data/test/dummy/app/views/meta_type_properties/_meta_type_property.html.erb +10 -0
- data/test/dummy/app/views/meta_type_properties/edit.html.erb +5 -0
- data/test/dummy/app/views/meta_type_properties/index.html.erb +9 -0
- data/test/dummy/app/views/meta_type_properties/new.html.erb +5 -0
- data/test/dummy/app/views/meta_types/_meta_type.html.erb +6 -0
- data/test/dummy/app/views/meta_types/edit.html.erb +5 -0
- data/test/dummy/app/views/meta_types/index.html.erb +9 -0
- data/test/dummy/app/views/meta_types/new.html.erb +5 -0
- data/test/dummy/app/views/things/_thing.html.erb +10 -0
- data/test/dummy/app/views/things/edit.html.erb +5 -0
- data/test/dummy/app/views/things/index.html.erb +9 -0
- data/test/dummy/app/views/things/new.html.erb +12 -0
- data/test/dummy/app/views/things/show.html.erb +10 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +58 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +67 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +15 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/simple_form.rb +178 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/locales/simple_form.en.yml +26 -0
- data/test/dummy/config/routes.rb +6 -0
- data/test/dummy/db/migrate/20120507130230_create_hstore.rb +9 -0
- data/test/dummy/db/migrate/20120507130237_meta_type.rb +19 -0
- data/test/dummy/db/migrate/20120507130255_meta_type_property.rb +23 -0
- data/test/dummy/db/migrate/20120507130303_meta_type_member.rb +18 -0
- data/test/dummy/db/migrate/20120508143058_create_things.rb +14 -0
- data/test/dummy/db/seeds.rb +28 -0
- data/test/dummy/db/structure.sql +309 -0
- data/test/dummy/lib/templates/erb/scaffold/_form.html.erb +13 -0
- data/test/dummy/log/development.log +8958 -0
- data/test/dummy/log/test.log +5559 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +25 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/dummy/tmp/cache/assets/C7F/AF0/sprockets%2F1f64b7e05e310702c90b98e4816685a2 +0 -0
- data/test/dummy/tmp/cache/assets/C94/420/sprockets%2F639d84895339654f12ac2bde8292d447 +0 -0
- data/test/dummy/tmp/cache/assets/CB1/FD0/sprockets%2F6e1bd95023705b5529e7ccc754a02867 +0 -0
- data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
- data/test/dummy/tmp/cache/assets/CFF/E00/sprockets%2F352bab412d75fa19d0a07504553b59df +0 -0
- data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/test/dummy/tmp/cache/assets/D49/8D0/sprockets%2F92613a75279536c4bcf4f3ba6cfde494 +0 -0
- data/test/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/test/dummy/tmp/cache/assets/D59/6F0/sprockets%2F7c015202319d3bb46ea41d4ff9b5ac0d +0 -0
- data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/test/dummy/tmp/cache/assets/D9F/640/sprockets%2Fbbc63c99f5e0eef2a890442ea3431dd7 +0 -0
- data/test/dummy/tmp/cache/assets/DB1/370/sprockets%2F18d79bbfdd80aa3bde79ab687266d992 +0 -0
- data/test/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/test/dummy/tmp/cache/assets/E02/0E0/sprockets%2Fa8f15cbf61c3d8edb5a99dbafd690721 +0 -0
- data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/meta_types_test.rb +7 -0
- data/test/test_helper.rb +15 -0
- data/test/unit/meta_types/meta_type_test.rb +181 -0
- metadata +257 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Peter Horn, metaminded UG
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
= MetaTypes
|
2
|
+
|
3
|
+
This is like {hstore}[http://railscasts.com/episodes/345-hstore] on steroids ;) It actually uses hstore for storage.
|
4
|
+
|
5
|
+
There is a {demo app on heroku}[http://meta-types.herokuapp.com/] ({code is here}[https://github.com/no-dashes/meta_types_demo])
|
6
|
+
|
7
|
+
== Mission Objective
|
8
|
+
|
9
|
+
As in the railscast mentioned above, you frequently get in the situation to store values in the database without knowing the exact structure of the required data in advance. This is one of the really cool features of NoSQL databases like MongoDB.
|
10
|
+
|
11
|
+
We often find that we need to configure the structure interactively. In traditional SQL databases, the {EAV-Pattern}[http://en.wikipedia.org/wiki/Entity–attribute–value_model] (or antipattern, rather), is used, for example to configure product types in {Magento}[http://www.magentocommerce.com/]. This leads to zillions of tables and to <b>really ugly</b> queries, which need to be aggressively cached for performance reasons.
|
12
|
+
|
13
|
+
Postgres offers THE SOLUTION to this problem, and we tried to make usage as simple as possible.
|
14
|
+
|
15
|
+
This is currently quite work-in-progress... Let's see how things evolve :)
|
16
|
+
|
17
|
+
== Usage
|
18
|
+
|
19
|
+
Create a table that has a <tt>yourcoice_hstore</tt> column, in this case, we use <tt>properties_hstore</tt>
|
20
|
+
|
21
|
+
class CreateThings < ActiveRecord::Migration
|
22
|
+
def change
|
23
|
+
execute <<-SQL
|
24
|
+
create table things(
|
25
|
+
id serial primary key,
|
26
|
+
meta_type_id integer references meta_types(id),
|
27
|
+
title character varying,
|
28
|
+
properties_hstore hstore,
|
29
|
+
created_at timestamp without time zone,
|
30
|
+
updated_at timestamp without time zone
|
31
|
+
)
|
32
|
+
SQL
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
in the corresponding class, we declare that we want to mount the meta_type information with the name <tt>properties</tt>.
|
37
|
+
|
38
|
+
class Thing < ActiveRecord::Base
|
39
|
+
attr_accessible :titile, :meta_type
|
40
|
+
|
41
|
+
meta_typed :properties
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
Then, get the necessary migrations:
|
46
|
+
|
47
|
+
$ rake meta_types:install:migrations
|
48
|
+
|
49
|
+
which installs the create table migrations for <tt>meta_type</tt>, <tt>meta_type_property</tt> and the mapping table <tt>meta_type_member</tt>.
|
50
|
+
|
51
|
+
We then define a <tt>MetaType</tt>: (<tt>sid</tt> is string-id ;))
|
52
|
+
|
53
|
+
typ = MetaType.new(sid: 'address', title: 'My Fancy Address')
|
54
|
+
typ.meta_type_properties = [
|
55
|
+
MetaTypeProperty.new(
|
56
|
+
sid: 'age',
|
57
|
+
label: 'Age',
|
58
|
+
property_type_sid: 'integer',
|
59
|
+
default_value: '10'
|
60
|
+
),
|
61
|
+
MetaTypeProperty.new(
|
62
|
+
sid: 'name',
|
63
|
+
label: 'Name',
|
64
|
+
property_type_sid: 'string'
|
65
|
+
),
|
66
|
+
MetaTypeProperty.new(
|
67
|
+
sid: 'gender',
|
68
|
+
label: 'Gender',
|
69
|
+
property_type_sid: 'string',
|
70
|
+
choices: 'male||female||other'
|
71
|
+
),
|
72
|
+
MetaTypeProperty.new(
|
73
|
+
sid: 'active',
|
74
|
+
label: 'Active',
|
75
|
+
property_type_sid: 'boolean',
|
76
|
+
default_value: 'true'
|
77
|
+
)
|
78
|
+
]
|
79
|
+
|
80
|
+
the available types for <tt>property_type_sid</tt> are
|
81
|
+
* integer
|
82
|
+
* string
|
83
|
+
* text (multi-line stings, the only difference is the way that simple_form renders the input)
|
84
|
+
* boolean
|
85
|
+
* float
|
86
|
+
* date
|
87
|
+
|
88
|
+
If the <tt>choices</tt> are set, <tt>simple_form</tt> can easily offer an select-box as input. The individual values are separated by ||.
|
89
|
+
|
90
|
+
You can now use the defined <tt>meta_type</tt> to add the defined attributes to another model:
|
91
|
+
|
92
|
+
thing = Thing.new(title: "MetaTypes Rock!", meta_type: typ)
|
93
|
+
assert thing.save
|
94
|
+
|
95
|
+
the defined attributes are available on the <tt>properties</tt> pseudo-relation of the <tt>thing</tt>, e.g.:
|
96
|
+
|
97
|
+
thing.properties.age == 10
|
98
|
+
thing.properties.active == true
|
99
|
+
|
100
|
+
and using <tt>properties[sid]</tt>, you can access the <tt>MetaPeroperty</tt> object encapsulating the actual data and attribute definition, e.g.:
|
101
|
+
|
102
|
+
thing.properties[:age].value == 10
|
103
|
+
thing.properties[:active].value == true
|
104
|
+
thing.properties[:active].label == 'Active'
|
105
|
+
thing.properties[:active].sid == 'active'
|
106
|
+
|
107
|
+
the properties can be set using
|
108
|
+
|
109
|
+
thing.properties.name = "Ryan Bates ;)"
|
110
|
+
thing.properties.gender = "male"
|
111
|
+
|
112
|
+
'Mass Assignment' also works so that you can construct a form like that:
|
113
|
+
|
114
|
+
<%= simple_form_for @thing do |f| %>
|
115
|
+
<%= f.input :meta_type_id, as: :hidden %>
|
116
|
+
<%= f.input :name %>
|
117
|
+
<%= f.fields_for :properties do |ff| %>
|
118
|
+
<% @thing.properties.each do |prop| %>
|
119
|
+
<%= ff.input prop.sid, collection: prop.choices, label: prop.label %>
|
120
|
+
<% end %>
|
121
|
+
<% end %>
|
122
|
+
<%= f.submit %>
|
123
|
+
<% end %>
|
124
|
+
|
125
|
+
or programmatically:
|
126
|
+
|
127
|
+
thing = Thing.new(
|
128
|
+
title: "Har har",
|
129
|
+
meta_type: typ,
|
130
|
+
properties_attributes: {
|
131
|
+
age: 125,
|
132
|
+
name: 'Methusalix',
|
133
|
+
gender: 'male',
|
134
|
+
active: true
|
135
|
+
}
|
136
|
+
)
|
137
|
+
|
138
|
+
The same works with <tt>update_attributes</tt> so that no special care needs to be taken in the controllers.
|
139
|
+
|
140
|
+
== Querying/Finding
|
141
|
+
|
142
|
+
To allow easy finding, we add methods to the <tt>meta_typed</tt> model:
|
143
|
+
|
144
|
+
Thing.where_properties(age: 10)
|
145
|
+
Thing.where_properties_like(name: '%salix%')
|
146
|
+
|
147
|
+
They may of course be chained for they are just ARels on <tt>Thing</tt>
|
148
|
+
|
149
|
+
== How is it done?
|
150
|
+
|
151
|
+
In the <tt>hstore</tt> column, the properties are saved in the form
|
152
|
+
|
153
|
+
sid -> value
|
154
|
+
|
155
|
+
where <tt>sid</tt> is the sid of the <tt>meta_type_property</tt> and the value (can only be a string in the db) is automatically converted.
|
156
|
+
|
157
|
+
== Demo App (seriously!)
|
158
|
+
|
159
|
+
There is a {demo app on heroku}[http://meta-types.herokuapp.com/] ({code is here}[https://github.com/no-dashes/meta_types_demo])
|
160
|
+
|
161
|
+
== Bugs!
|
162
|
+
|
163
|
+
There are roughly another 997 bugs in meta_types, although we do some testing (see <tt>test/</tt>). So if you hunt them, please let me know using the {GitHub Bugtracker}[https://github.com/metaminded/meta_types/issues].
|
164
|
+
|
165
|
+
== Contributing to meta_types
|
166
|
+
|
167
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
168
|
+
* Check out the {issue tracker}[https://github.com/metaminded/meta_types/issues] to make sure someone already hasn't requested it and/or contributed it
|
169
|
+
* Fork the project
|
170
|
+
* Start a feature/bugfix branch
|
171
|
+
* Commit and push until you are happy with your contribution
|
172
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
173
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
174
|
+
* Feel free to send a pull request if you think others (me, for example) would like to have your change incorporated into future versions of meta_types.
|
175
|
+
|
176
|
+
== License
|
177
|
+
|
178
|
+
Copyright (c) 2012 Peter Horn, {metaminded UG}[http://www.metaminded.com]
|
179
|
+
|
180
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
181
|
+
|
182
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
183
|
+
|
184
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'MetaTypes'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
24
|
+
load 'rails/tasks/engine.rake'
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
Bundler::GemHelper.install_tasks
|
29
|
+
|
30
|
+
require 'rake/testtask'
|
31
|
+
|
32
|
+
Rake::TestTask.new(:test) do |t|
|
33
|
+
t.libs << 'lib'
|
34
|
+
t.libs << 'test'
|
35
|
+
t.pattern = 'test/**/*_test.rb'
|
36
|
+
t.verbose = false
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
task :default => :test
|
@@ -0,0 +1,15 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// the compiled file.
|
9
|
+
//
|
10
|
+
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
|
11
|
+
// GO AFTER THE REQUIRES BELOW.
|
12
|
+
//
|
13
|
+
//= require jquery
|
14
|
+
//= require jquery_ujs
|
15
|
+
//= require_tree .
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
9
|
+
* compiled file, but it's generally better to create a new file per style scope.
|
10
|
+
*
|
11
|
+
*= require_self
|
12
|
+
*= require_tree .
|
13
|
+
*/
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# (c) 2012 metaminded UG, all rights reserved
|
4
|
+
|
5
|
+
<<-CREATE_SQL
|
6
|
+
create table meta_types (
|
7
|
+
id serial PRIMARY KEY,
|
8
|
+
sid character varying not null UNIQUE,
|
9
|
+
title character varying not null UNIQUE,
|
10
|
+
created_at timestamp without time zone,
|
11
|
+
updated_at timestamp without time zone
|
12
|
+
);
|
13
|
+
CREATE_SQL
|
14
|
+
|
15
|
+
class MetaType < ActiveRecord::Base
|
16
|
+
|
17
|
+
attr_accessible :sid, :title, :meta_type_property_ids
|
18
|
+
|
19
|
+
has_many :meta_type_members
|
20
|
+
has_many :meta_type_properties, :through => :meta_type_members
|
21
|
+
|
22
|
+
accepts_nested_attributes_for :meta_type_members
|
23
|
+
accepts_nested_attributes_for :meta_type_properties
|
24
|
+
|
25
|
+
validates_presence_of :title
|
26
|
+
validates_uniqueness_of :title
|
27
|
+
validates_presence_of :sid
|
28
|
+
validates_uniqueness_of :sid
|
29
|
+
|
30
|
+
# class methods
|
31
|
+
class << self
|
32
|
+
def [](sid) find_by_sid(sid); end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# (c) 2012 metaminded UG, all rights reserved
|
4
|
+
|
5
|
+
<<-CREATE_SQL
|
6
|
+
create table meta_type_members (
|
7
|
+
id serial PRIMARY KEY,
|
8
|
+
meta_type_id integer references meta_types(id),
|
9
|
+
meta_type_property_id integer references meta_type_properties(id),
|
10
|
+
position integer,
|
11
|
+
created_at timestamp without time zone,
|
12
|
+
updated_at timestamp without time zone
|
13
|
+
);
|
14
|
+
CREATE_SQL
|
15
|
+
|
16
|
+
class MetaTypeMember < ActiveRecord::Base
|
17
|
+
|
18
|
+
attr_accessible :position
|
19
|
+
|
20
|
+
belongs_to :meta_type
|
21
|
+
belongs_to :meta_type_property
|
22
|
+
|
23
|
+
after_create do
|
24
|
+
self.position ||= self.id
|
25
|
+
self.save!
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# (c) 2009-2011 metaminded UG, all rights reserved
|
3
|
+
|
4
|
+
<<-CREATE_SQL
|
5
|
+
create table meta_type_properties (
|
6
|
+
id serial NOT NULL PRIMARY KEY,
|
7
|
+
sid character varying,
|
8
|
+
label character varying not null,
|
9
|
+
property_type_sid character varying not null,
|
10
|
+
required boolean not null default false,
|
11
|
+
system boolean not null default false,
|
12
|
+
dimension character varying null default null,
|
13
|
+
default_value character varying null default null,
|
14
|
+
created_at timestamp without time zone,
|
15
|
+
updated_at timestamp without time zone
|
16
|
+
);
|
17
|
+
CREATE_SQL
|
18
|
+
|
19
|
+
class MetaTypeProperty < ActiveRecord::Base
|
20
|
+
|
21
|
+
attr_accessible :sid, :label, :property_type_sid, :default_value, :required, :dimension, :choices
|
22
|
+
|
23
|
+
# Associations
|
24
|
+
has_many :meta_type_members
|
25
|
+
has_many :meta_types, :through => :meta_type_members
|
26
|
+
|
27
|
+
# Validations
|
28
|
+
validates_presence_of :label
|
29
|
+
validates_presence_of :property_type_sid
|
30
|
+
validates_inclusion_of :property_type_sid, in: MetaTypes::MetaPropertyType.sids
|
31
|
+
validates_format_of :sid, with: /\A[a-z][a-z_0-9]*\Z/
|
32
|
+
validates_exclusion_of :sid, in: %w{alias and begin break case class def defined?
|
33
|
+
do else elsif end ensure false for if in module next nil not or redo rescue retry
|
34
|
+
return self super then true undef unless until when while yield},
|
35
|
+
message: "is a ruby keyword, please don't use that."
|
36
|
+
validates_exclusion_of :sid, in: MetaTypes::MetaProperties.instance_methods.map(&:to_s),
|
37
|
+
message: "overrides core functionality, please don't use that."
|
38
|
+
|
39
|
+
scope :ordered, order("meta_type_members.position asc")
|
40
|
+
|
41
|
+
def name() "#{label} (#{property_type_sid}) " end
|
42
|
+
|
43
|
+
def property_type
|
44
|
+
MetaTypes::MetaPropertyType[property_type_sid]
|
45
|
+
end
|
46
|
+
|
47
|
+
def set_position_for!(meta_type, pos)
|
48
|
+
meta_type_member = meta_type_members.where(meta_type_id: meta_type.id).first
|
49
|
+
meta_type_member.position = pos
|
50
|
+
meta_type_member.save!
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_position_for(meta_type)
|
54
|
+
meta_type_member = meta_type_members.where(meta_type_id: meta_type.id).first
|
55
|
+
meta_type_member.position
|
56
|
+
end
|
57
|
+
|
58
|
+
def type
|
59
|
+
{integer: :numeric}[property_type_sid.to_sym] || property_type_sid.to_sym
|
60
|
+
end
|
61
|
+
|
62
|
+
def number?
|
63
|
+
%w{integer float}.member? property_type_sid
|
64
|
+
end
|
65
|
+
|
66
|
+
def limit() nil end
|
67
|
+
|
68
|
+
delegate :cast, :parse, to: :property_type
|
69
|
+
|
70
|
+
class << self
|
71
|
+
def [](sid) find_by_sid(sid); end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>MetaTypes</title>
|
5
|
+
<%= stylesheet_link_tag "meta_types/application", :media => "all" %>
|
6
|
+
<%= javascript_include_tag "meta_types/application" %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|