enum_table 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/Gemfile +3 -0
- data/lib/enum_table/record.rb +1 -1
- data/lib/enum_table/schema_dumper.rb +1 -1
- data/lib/enum_table/schema_statements.rb +5 -5
- data/lib/enum_table/version.rb +1 -1
- data/test/database.yml +1 -1
- data/test/enum_table/test_record.rb +12 -0
- data/test/enum_table/test_schema_dumper.rb +16 -2
- data/test/enum_table/test_schema_statements.rb +23 -4
- metadata +9 -4
data/CHANGELOG
CHANGED
data/Gemfile
CHANGED
data/lib/enum_table/record.rb
CHANGED
@@ -61,7 +61,7 @@ module EnumTable
|
|
61
61
|
map = {}
|
62
62
|
table_name = table || "#{self.table_name.singularize}_#{name.to_s.pluralize}"
|
63
63
|
return {} if EnumTable.missing_tables_allowed? && !connection.tables.include?(table_name)
|
64
|
-
connection.execute("SELECT id, value FROM #{table_name}").each do |row|
|
64
|
+
connection.execute("SELECT id, value FROM #{connection.quote_table_name table_name}").each do |row|
|
65
65
|
map[row[1]] = row[0]
|
66
66
|
end
|
67
67
|
map
|
@@ -13,7 +13,7 @@ module EnumTable
|
|
13
13
|
table_names.each do |table_name|
|
14
14
|
stream.puts " create_enum_table #{table_name.inspect}, force: true do |t|"
|
15
15
|
enum_table_column(stream, table_name, 'value', SchemaStatements::DEFAULT_VALUE_ATTRIBUTES)
|
16
|
-
@connection.execute("SELECT id, value FROM #{table_name} ORDER BY id").each do |row|
|
16
|
+
@connection.execute("SELECT id, value FROM #{@connection.quote_table_name table_name} ORDER BY id").each do |row|
|
17
17
|
stream.puts " t.add #{row[1].to_s.inspect}, #{row[0]}"
|
18
18
|
end
|
19
19
|
stream.puts " end"
|
@@ -13,7 +13,7 @@ module EnumTable
|
|
13
13
|
|
14
14
|
def drop_enum_table(table_name)
|
15
15
|
drop_table table_name
|
16
|
-
execute "DELETE FROM enum_tables WHERE table_name =
|
16
|
+
execute "DELETE FROM enum_tables WHERE table_name = #{quote table_name}"
|
17
17
|
enum_tables_updated
|
18
18
|
end
|
19
19
|
|
@@ -49,7 +49,7 @@ module EnumTable
|
|
49
49
|
t.string :table_name, null: false, limit: 255
|
50
50
|
end
|
51
51
|
end
|
52
|
-
@connection.execute "INSERT INTO enum_tables(table_name) VALUES(
|
52
|
+
@connection.execute "INSERT INTO enum_tables(table_name) VALUES(#{@connection.quote @name})"
|
53
53
|
table = Table.new(@connection, @name, 0)
|
54
54
|
@adds.each { |args| table.add(*args) }
|
55
55
|
end
|
@@ -67,17 +67,17 @@ module EnumTable
|
|
67
67
|
def initialize(connection, name, max_id=nil)
|
68
68
|
@connection = connection
|
69
69
|
@name = name
|
70
|
-
@max_id = @connection.execute("SELECT max(id) FROM #{@name}").to_a[0][0] || 0
|
70
|
+
@max_id = @connection.execute("SELECT max(id) FROM #{@connection.quote_table_name @name}").to_a[0][0] || 0
|
71
71
|
end
|
72
72
|
|
73
73
|
def add(value, id=nil)
|
74
74
|
id ||= @max_id + 1
|
75
75
|
@max_id = id if id > @max_id
|
76
|
-
@connection.execute "INSERT INTO #{@name}(id, value) VALUES(#{id},
|
76
|
+
@connection.execute "INSERT INTO #{@connection.quote_table_name @name}(id, value) VALUES(#{id}, #{@connection.quote value})"
|
77
77
|
end
|
78
78
|
|
79
79
|
def remove(value)
|
80
|
-
@connection.execute "DELETE FROM #{@name} WHERE value =
|
80
|
+
@connection.execute "DELETE FROM #{@connection.quote_table_name @name} WHERE value = #{@connection.quote value}"
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
data/lib/enum_table/version.rb
CHANGED
data/test/database.yml
CHANGED
@@ -31,6 +31,18 @@ describe EnumTable do
|
|
31
31
|
reflection.name.must_equal :gender
|
32
32
|
end
|
33
33
|
|
34
|
+
it "performs the necessary SQL-escaping when reading the table" do
|
35
|
+
connection.create_table("a'b") { |t| t.string :value }
|
36
|
+
connection.execute "INSERT INTO `a'b`(id, value) VALUES (1, 'c''d')"
|
37
|
+
Object.const_set :AB, Class.new(ActiveRecord::Base) { self.table_name = "a'b" }
|
38
|
+
begin
|
39
|
+
AB.enum :e, table: "a'b"
|
40
|
+
AB.enums[:e].value(1).must_equal(:"c'd")
|
41
|
+
ensure
|
42
|
+
Object.send :remove_const, :AB
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
34
46
|
it "accepts the :table name as a string" do
|
35
47
|
connection.create_table(:custom_table) { |t| t.string :value }
|
36
48
|
connection.execute "INSERT INTO custom_table(id, value) VALUES (1, 'male'), (2, 'female')"
|
@@ -37,12 +37,26 @@ describe EnumTable::SchemaDumper do
|
|
37
37
|
|
38
38
|
it "dumps custom value column attributes" do
|
39
39
|
connection.create_enum_table :user_genders do |t|
|
40
|
-
t.value type: :
|
40
|
+
t.value type: :integer, limit: 2, null: true
|
41
41
|
end
|
42
42
|
ActiveRecord::SchemaDumper.dump(connection, stream)
|
43
43
|
stream.string.must_include(<<-EOS.gsub(/^ *\|/, ''))
|
44
44
|
| create_enum_table "user_genders", force: true do |t|
|
45
|
-
| t.value type: :
|
45
|
+
| t.value type: :integer, limit: 2, null: true
|
46
|
+
| end
|
47
|
+
EOS
|
48
|
+
end
|
49
|
+
|
50
|
+
it "performs the necessary SQL-escaping when reading tables" do
|
51
|
+
connection.instance_eval do
|
52
|
+
create_enum_table "a'b" do |t|
|
53
|
+
t.add "c'd", 1
|
54
|
+
end
|
55
|
+
end
|
56
|
+
ActiveRecord::SchemaDumper.dump(connection, stream)
|
57
|
+
stream.string.must_include(<<-EOS.gsub(/^ *\|/, ''))
|
58
|
+
| create_enum_table "a'b", force: true do |t|
|
59
|
+
| t.add "c'd", 1
|
46
60
|
| end
|
47
61
|
EOS
|
48
62
|
end
|
@@ -30,11 +30,11 @@ describe EnumTable::SchemaStatements do
|
|
30
30
|
|
31
31
|
it "allows configuring the type column in the block" do
|
32
32
|
connection.create_enum_table :genders do |t|
|
33
|
-
t.value type: :
|
33
|
+
t.value type: :integer, limit: 2
|
34
34
|
end
|
35
35
|
column = connection.columns(:genders).find { |c| c.name == 'value' }
|
36
|
-
column.type.must_equal :
|
37
|
-
column.limit.must_equal
|
36
|
+
column.type.must_equal :integer
|
37
|
+
column.limit.must_equal 2
|
38
38
|
end
|
39
39
|
|
40
40
|
it "records the enum_table" do
|
@@ -55,6 +55,13 @@ describe EnumTable::SchemaStatements do
|
|
55
55
|
end
|
56
56
|
read_table('genders').must_equal [[1, 'female'], [2, 'male']]
|
57
57
|
end
|
58
|
+
|
59
|
+
it "performs the necessary SQL escaping in value names" do
|
60
|
+
connection.create_enum_table "a'b" do |t|
|
61
|
+
t.add "c'd", 1
|
62
|
+
end
|
63
|
+
read_table("a'b").must_equal [[1, "c'd"]]
|
64
|
+
end
|
58
65
|
end
|
59
66
|
|
60
67
|
describe "#change_enum_table" do
|
@@ -83,6 +90,18 @@ describe EnumTable::SchemaStatements do
|
|
83
90
|
end
|
84
91
|
read_table('genders').must_equal [[1, 'female']]
|
85
92
|
end
|
93
|
+
|
94
|
+
it "performs the necessary SQL escaping in value names" do
|
95
|
+
connection.create_enum_table "a'b" do |t|
|
96
|
+
t.add "c'd", 1
|
97
|
+
end
|
98
|
+
|
99
|
+
connection.change_enum_table "a'b" do |t|
|
100
|
+
t.remove "c'd"
|
101
|
+
t.add "e'f", 1
|
102
|
+
end
|
103
|
+
read_table("a'b").must_equal [[1, "e'f"]]
|
104
|
+
end
|
86
105
|
end
|
87
106
|
|
88
107
|
describe "#drop_enum_table" do
|
@@ -129,7 +148,7 @@ describe EnumTable::SchemaStatements do
|
|
129
148
|
|
130
149
|
def read_table(name)
|
131
150
|
rows = []
|
132
|
-
connection.execute("SELECT id, value FROM #{name} ORDER BY id").each do |row|
|
151
|
+
connection.execute("SELECT id, value FROM #{connection.quote_table_name name} ORDER BY id").each do |row|
|
133
152
|
rows << [row[0], row[1]]
|
134
153
|
end
|
135
154
|
rows
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enum_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -82,15 +82,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
82
82
|
- - ! '>='
|
83
83
|
- !ruby/object:Gem::Version
|
84
84
|
version: '0'
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
hash: -950106288231480747
|
85
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
89
|
none: false
|
87
90
|
requirements:
|
88
91
|
- - ! '>='
|
89
92
|
- !ruby/object:Gem::Version
|
90
93
|
version: '0'
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
hash: -950106288231480747
|
91
97
|
requirements: []
|
92
98
|
rubyforge_project:
|
93
|
-
rubygems_version: 1.8.
|
99
|
+
rubygems_version: 1.8.25
|
94
100
|
signing_key:
|
95
101
|
specification_version: 3
|
96
102
|
summary: Enumeration tables for ActiveRecord
|
@@ -101,4 +107,3 @@ test_files:
|
|
101
107
|
- test/enum_table/test_schema_dumper.rb
|
102
108
|
- test/enum_table/test_schema_statements.rb
|
103
109
|
- test/test_helper.rb
|
104
|
-
has_rdoc:
|