belongs_to_enum 0.1 → 0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +41 -1
- data/lib/belongs_to_enum/enum.rb +2 -2
- data/lib/belongs_to_enum/version.rb +1 -1
- data/spec/belongs_to_enum.rb +4 -4
- metadata +2 -2
data/README
CHANGED
@@ -1,2 +1,42 @@
|
|
1
1
|
Adds support for transparent enums to ActiveRecord.
|
2
|
-
Adds a belongs_to_enum to ActiveRecord that takes an array of possible values.
|
2
|
+
Adds a belongs_to_enum to ActiveRecord that takes an array of possible values.
|
3
|
+
|
4
|
+
Adds an add_extra_data method to ActiveRecord that invisibly includes an extra data table. Use with STI to keep your database clean. It removes the need to have lots of nullable columns.
|
5
|
+
|
6
|
+
Usage
|
7
|
+
---------
|
8
|
+
Use in Rails 3 app. Add to bundler:
|
9
|
+
gem "belongs_to_enum"
|
10
|
+
|
11
|
+
Tests
|
12
|
+
---------
|
13
|
+
Uses rspec for testing.
|
14
|
+
Run tests with rspec spec/*
|
15
|
+
|
16
|
+
Todo
|
17
|
+
---------
|
18
|
+
* Add scopes
|
19
|
+
|
20
|
+
Example
|
21
|
+
---------
|
22
|
+
|
23
|
+
Usage:
|
24
|
+
|
25
|
+
In models:
|
26
|
+
scope :admin, where(:role_id => User::Role.admin)
|
27
|
+
|
28
|
+
In views:
|
29
|
+
=f.select(:role, User::Role.pretty_items)
|
30
|
+
-User::Role.each do |role_title, role_id|
|
31
|
+
#{role_title} (#{role_id})
|
32
|
+
|
33
|
+
Classes:
|
34
|
+
class User < ActiveRecord::Base
|
35
|
+
belongs_to_enum :role, [:normal, :admin, :superadmin, :suspended]
|
36
|
+
end
|
37
|
+
|
38
|
+
Database Schema:
|
39
|
+
|
40
|
+
create_table :users do |t|
|
41
|
+
t.float :role_id
|
42
|
+
end
|
data/lib/belongs_to_enum/enum.rb
CHANGED
@@ -26,13 +26,13 @@ module BelongsToEnum
|
|
26
26
|
@hash.values
|
27
27
|
end
|
28
28
|
|
29
|
-
def Enum.
|
29
|
+
def Enum.items_for_select
|
30
30
|
@hash.sort{|a,b| a[1]<=>b[1]}.map{|i|[i[0].to_s.gsub("_", " ").capitalize,i[1]]}
|
31
31
|
end
|
32
32
|
|
33
33
|
def Enum.items
|
34
34
|
warn "DEPRECIATED belongs_to_enum: Use pretty items instead"
|
35
|
-
|
35
|
+
items_for_select
|
36
36
|
end
|
37
37
|
|
38
38
|
def Enum.get(value)
|
data/spec/belongs_to_enum.rb
CHANGED
@@ -26,12 +26,12 @@ describe "Belongs To Enum models" do
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
it "has
|
30
|
-
User::Role.
|
31
|
-
User::Role.
|
29
|
+
it "has items_for_select" do
|
30
|
+
User::Role.items_for_select.first[0].should == "Normal"
|
31
|
+
User::Role.items_for_select.first[1].should == 1
|
32
32
|
end
|
33
33
|
|
34
|
-
it "has depreciated items method that maps to
|
34
|
+
it "has depreciated items method that maps to items_for_select" do
|
35
35
|
User::Role.items.first[0].should == "Normal"
|
36
36
|
User::Role.items.first[1].should == 1
|
37
37
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: belongs_to_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: "0.
|
5
|
+
version: "0.2"
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jeremy walker
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-18 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|