toy-attributes 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +83 -0
- data/lib/toy-attributes/relations.rb +6 -3
- data/lib/toy-attributes/version.rb +1 -1
- metadata +13 -13
- data/README.rdoc +0 -3
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
Toy Attributes
|
2
|
+
==============
|
3
|
+
|
4
|
+
Toy Attributes is a component of [Toy Locomotive](https://github.com/Mortaro/Toy-Locomotive]), but can be used independently outside Toy Locomotive.
|
5
|
+
|
6
|
+
|
7
|
+
Getting Started
|
8
|
+
---------------
|
9
|
+
|
10
|
+
All you need to start using Toy Attributes is to add the following line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem "toy-attributes"
|
13
|
+
|
14
|
+
|
15
|
+
Declaring Attributes
|
16
|
+
--------------------
|
17
|
+
|
18
|
+
This gem allows to assign attributes directly inside ActiveRecord models without having to write migrations. In order to make it happen, your model will have access to the following class methods:
|
19
|
+
|
20
|
+
|
21
|
+
string, text, integer, float, decimal, datetime, timestamp, time, date, binary, boolean, references
|
22
|
+
|
23
|
+
Let's say your application is about warriors and all your warriors have a name, the code bellow will do the trick:
|
24
|
+
|
25
|
+
class Warrior < ActiveRecord::Base
|
26
|
+
string :name
|
27
|
+
end
|
28
|
+
|
29
|
+
With this snippet, Toy Attributes will create the table `warriors` if it does not exist already, and add the `string` column `name` to it. You can also pass more than one column at a time to your method, for example:
|
30
|
+
|
31
|
+
class Weapon < ActiveRecord::Base
|
32
|
+
integer :min_damage, :max_damage
|
33
|
+
end
|
34
|
+
|
35
|
+
Now both `min_damage` and `max_damage` will be present on `weapons` table's schema and ready to use.
|
36
|
+
|
37
|
+
*All attributes generated with Toy are declared as `attr_accessible`, making them mass-assignable.*
|
38
|
+
|
39
|
+
|
40
|
+
Dealing with Relations
|
41
|
+
----------------------
|
42
|
+
|
43
|
+
Toy also deals with model relations in a simple way, so you won't have to worry about their migrations either.
|
44
|
+
|
45
|
+
class Warrior < ActiveRecord::Base
|
46
|
+
string :name
|
47
|
+
has_many :weapons
|
48
|
+
end
|
49
|
+
|
50
|
+
class Weapon < ActiveRecord::Base
|
51
|
+
integer :min_damage, :max_damage
|
52
|
+
end
|
53
|
+
|
54
|
+
Note that you won't have to declare that weapon `belongs_to` a warrior, Toy will automatically generate the foreign key and set up the relation; the same works to `has_one` relations.
|
55
|
+
|
56
|
+
You can also declare multiple relations in a single line and even pass a hash of options:
|
57
|
+
|
58
|
+
class Warrior < ActiveRecord::Base
|
59
|
+
has_many :weapons, :items, :dependent => :destroy
|
60
|
+
end
|
61
|
+
|
62
|
+
*All relations generated by Toy Attributes will `accept_nested_attributes_for` and receive `attr_accessible` on related model's attributes.*
|
63
|
+
|
64
|
+
|
65
|
+
Changing Columns
|
66
|
+
----------------
|
67
|
+
|
68
|
+
At development mode, any change on your models will be automatically detected and instantly applied:
|
69
|
+
|
70
|
+
class Weapon < ActiveRecord::Base
|
71
|
+
float :min_damage, :max_damage
|
72
|
+
end
|
73
|
+
|
74
|
+
This will result on a change of column type at the very moment it's identified on development mode or in the startup in production mode.
|
75
|
+
|
76
|
+
TO-DO
|
77
|
+
-----
|
78
|
+
|
79
|
+
* Add a rake task to clean unused columns;
|
80
|
+
* Allow more options on columns creation;
|
81
|
+
* Generate hardcoded migrations on db/migrations and make them reversible;
|
82
|
+
* Add support to habtm relations;
|
83
|
+
* Make automatic mass-assignment configurable via initializer.
|
@@ -6,9 +6,12 @@ module ToyAttributes::Relations
|
|
6
6
|
alias :_has_many :has_many
|
7
7
|
alias :_has_one :has_one
|
8
8
|
|
9
|
-
def has_many
|
10
|
-
|
11
|
-
|
9
|
+
def has_many *models
|
10
|
+
options = models.extract_options!
|
11
|
+
models.each do |model|
|
12
|
+
self._has_many model, options
|
13
|
+
enforce_relation_with model
|
14
|
+
end
|
12
15
|
end
|
13
16
|
|
14
17
|
def has_one model
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: toy-attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &30063924 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *30063924
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec-rails
|
27
|
-
requirement: &
|
27
|
+
requirement: &30063408 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *30063408
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: shoulda
|
38
|
-
requirement: &
|
38
|
+
requirement: &30063156 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *30063156
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: sqlite3
|
49
|
-
requirement: &
|
49
|
+
requirement: &30079272 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *30079272
|
58
58
|
description: Allows to assign attributes directly inside ActiveRecord models without
|
59
59
|
having to write migrations
|
60
60
|
email:
|
@@ -72,7 +72,7 @@ files:
|
|
72
72
|
- lib/toy-attributes.rb
|
73
73
|
- MIT-LICENSE
|
74
74
|
- Rakefile
|
75
|
-
- README.
|
75
|
+
- README.md
|
76
76
|
homepage: https://github.com/Mortaro/Toy-Attributes
|
77
77
|
licenses: []
|
78
78
|
post_install_message:
|
@@ -87,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
87
|
version: '0'
|
88
88
|
segments:
|
89
89
|
- 0
|
90
|
-
hash:
|
90
|
+
hash: -539771597
|
91
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
92
|
none: false
|
93
93
|
requirements:
|
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
96
|
version: '0'
|
97
97
|
segments:
|
98
98
|
- 0
|
99
|
-
hash:
|
99
|
+
hash: -539771597
|
100
100
|
requirements: []
|
101
101
|
rubyforge_project:
|
102
102
|
rubygems_version: 1.8.16
|
data/README.rdoc
DELETED