superstore 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8332e4f8d43a68c737e31ae1a88a89c360782a88
4
- data.tar.gz: 9cbbc5a3fa095a0af7ef48fb265460530a2a5191
3
+ metadata.gz: ab95c845f31fa3557952125e9db3fb68ec37d8d9
4
+ data.tar.gz: 5bea5b3c94a7c172bd6ce97a4a21ea1b5d77a96f
5
5
  SHA512:
6
- metadata.gz: b09a471630f8a389b2cb250ab70b4d5f1ed48e67ebceb828d9fe1b13236d88bda6ca89c09b3153a70a41f225c6ef25861b7857c3cfeb9bf68bd37c7440e39dad
7
- data.tar.gz: 6d68424187b262902591482f8f2db73a40cf7f0bcba670b6eea343538d0c7ca8cd09ff56d8fd11e1955c944a517353fa3243654600f431c21405a97c929aa161
6
+ metadata.gz: bf14d4bf4b8fd2d03e1e7627c9facff10ba47d454d2a43e0f26956526112b8914fd4981c56ee6100cce315763e4338c54b03f63c6305ee78ec13aae3a28dfa32
7
+ data.tar.gz: 07502ca8fea646f7041ef1c1ad0be27dd643f0dc751eb0a55350a4d8c0d22258c5767afc9fcfc03438c122f125b3301ed0b890cf770adaddefcd9f5c2a29d998
data/Gemfile CHANGED
@@ -6,14 +6,11 @@ gem 'thin'
6
6
 
7
7
  group :test do
8
8
  gem 'rails'
9
+ gem 'pg'
10
+ gem 'activerecord'
9
11
  gem 'mocha', require: false
10
12
  end
11
13
 
12
14
  group :cassandra do
13
- gem 'cassandra-cql'
14
- end
15
-
16
- group :hstore do
17
- gem 'activerecord'
18
- gem 'pg'
15
+ gem 'cassandra-cql', "1.1.4"
19
16
  end
@@ -12,11 +12,8 @@ module Superstore
12
12
 
13
13
  def reader
14
14
  unless loaded?
15
- if record_id = owner.send(reflection.foreign_key).presence
16
- self.record_variable = association_class.find_by_id(record_id)
17
- else
18
- self.record_variable = nil
19
- end
15
+ check_appropriate_primary_key!
16
+ self.record_variable = get_record
20
17
  @loaded = true
21
18
  end
22
19
 
@@ -24,9 +21,10 @@ module Superstore
24
21
  end
25
22
 
26
23
  def writer(record)
24
+ check_appropriate_primary_key!
27
25
  self.record_variable = record
28
26
  @loaded = true
29
- owner.send("#{reflection.foreign_key}=", record.try(:id))
27
+ owner.send("#{reflection.foreign_key}=", record.try(reflection.primary_key))
30
28
  if reflection.polymorphic?
31
29
  owner.send("#{reflection.polymorphic_column}=", record.class.name)
32
30
  end
@@ -43,6 +41,25 @@ module Superstore
43
41
  def loaded?
44
42
  @loaded
45
43
  end
44
+
45
+ private
46
+
47
+ def get_record
48
+ record_id = owner.send(reflection.foreign_key).presence
49
+ return unless record_id
50
+
51
+ if reflection.default_primary_key?
52
+ association_class.find_by_id(record_id)
53
+ else
54
+ association_class.find_by(reflection.primary_key => record_id)
55
+ end
56
+ end
57
+
58
+ def check_appropriate_primary_key!
59
+ if !reflection.default_primary_key? && !(association_class <= ActiveRecord::Base)
60
+ raise ArgumentError, "Association must inherit from ActiveRecord::Base to use custom primary key"
61
+ end
62
+ end
46
63
  end
47
64
  end
48
65
  end
@@ -11,7 +11,15 @@ module Superstore
11
11
  end
12
12
 
13
13
  def foreign_key
14
- "#{name}_id"
14
+ options[:foreign_key] || "#{name}_id"
15
+ end
16
+
17
+ def primary_key
18
+ options[:primary_key] || "id"
19
+ end
20
+
21
+ def default_primary_key?
22
+ primary_key == "id"
15
23
  end
16
24
 
17
25
  def polymorphic_column
@@ -6,8 +6,8 @@ module Superstore
6
6
 
7
7
  initializer "superstore.config" do |app|
8
8
  ActiveSupport.on_load :superstore do
9
- pathname = Rails.root.join('config', 'superstore.yml')
10
- if pathname.exist?
9
+ pathnames = [Rails.root.join('config', 'superstore.yml'), Rails.root.join('config', 'cassandra.yml')]
10
+ if pathname = pathnames.detect(&:exist?)
11
11
  config = YAML.load(pathname.read)
12
12
 
13
13
  if config = config[Rails.env]
data/superstore.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'superstore'
5
- s.version = '1.0.0'
5
+ s.version = '1.0.2'
6
6
  s.description = 'ActiveModel for many attributes'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
  s.authors = ["Michael Koziarski", "gotime"]
@@ -17,6 +17,8 @@ end
17
17
  sleep 1
18
18
  Superstore::CassandraSchema.create_keyspace 'superstore_test'
19
19
  Superstore::CassandraSchema.create_column_family 'Issues'
20
+ Superstore::CassandraSchema.alter_column_family 'Issues', "ADD title varchar"
21
+ Superstore::CassandraSchema.add_index 'Issues', 'title', "issues_title_idx"
20
22
 
21
23
  Superstore::Base.class_eval do
22
24
  class_attribute :created_records
@@ -1,24 +1,8 @@
1
1
  Bundler.require :hstore
2
- require 'active_record'
3
-
4
2
  Superstore::Base.config = {'adapter' => 'hstore'}
5
3
 
6
- class PGInitializer
4
+ class HstoreInitializer
7
5
  def self.initialize!
8
- config = {
9
- 'adapter' => 'postgresql',
10
- 'encoding' => 'unicode',
11
- 'database' => 'superstore_test',
12
- 'pool' => 5,
13
- 'username' => 'postgres'
14
- }
15
-
16
- ActiveRecord::Base.configurations = { test: config }
17
-
18
- ActiveRecord::Tasks::DatabaseTasks.drop config
19
- ActiveRecord::Tasks::DatabaseTasks.create config
20
- ActiveRecord::Base.establish_connection config
21
-
22
6
  Superstore::Base.adapter.create_table('issues')
23
7
  end
24
8
 
@@ -27,14 +11,14 @@ class PGInitializer
27
11
  end
28
12
  end
29
13
 
30
- PGInitializer.initialize!
14
+ HstoreInitializer.initialize!
31
15
 
32
16
  module ActiveSupport
33
17
  class TestCase
34
18
  teardown do
35
- PGInitializer.table_names.each do |table_name|
19
+ HstoreInitializer.table_names.each do |table_name|
36
20
  ActiveRecord::Base.connection.execute "TRUNCATE #{table_name}"
37
21
  end
38
22
  end
39
23
  end
40
- end
24
+ end
@@ -0,0 +1,44 @@
1
+ require 'active_record'
2
+
3
+ class PGInitializer
4
+ def self.initialize!
5
+ config = {
6
+ 'adapter' => 'postgresql',
7
+ 'encoding' => 'unicode',
8
+ 'database' => 'superstore_test',
9
+ 'pool' => 5,
10
+ 'username' => 'postgres'
11
+ }
12
+
13
+ ActiveRecord::Base.configurations = { test: config }
14
+
15
+ ActiveRecord::Tasks::DatabaseTasks.drop config
16
+ ActiveRecord::Tasks::DatabaseTasks.create config
17
+ ActiveRecord::Base.establish_connection config
18
+
19
+ create_users_table
20
+ end
21
+
22
+ def self.create_users_table
23
+ ActiveRecord::Migration.create_table :users do |t|
24
+ t.string :special_id, null: false
25
+ t.index :special_id, unique: true
26
+ end
27
+ end
28
+
29
+ def self.table_names
30
+ %w(users)
31
+ end
32
+ end
33
+
34
+ PGInitializer.initialize!
35
+
36
+ module ActiveSupport
37
+ class TestCase
38
+ teardown do
39
+ PGInitializer.table_names.each do |table_name|
40
+ ActiveRecord::Base.connection.execute "TRUNCATE #{table_name}"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,2 @@
1
+ class User < ActiveRecord::Base
2
+ end
data/test/test_helper.rb CHANGED
@@ -6,9 +6,11 @@ I18n.config.enforce_available_locales = false
6
6
  require 'rails/test_help'
7
7
  require 'mocha/setup'
8
8
 
9
- # require 'support/hstore'
9
+ require 'support/pg'
10
+ #require 'support/hstore'
10
11
  require 'support/cassandra'
11
12
  require 'support/issue'
13
+ require 'support/user'
12
14
 
13
15
  def MiniTest.filter_backtrace(bt)
14
16
  bt
@@ -2,11 +2,11 @@ require 'test_helper'
2
2
 
3
3
  class Superstore::BelongsTo::ReflectionTest < Superstore::TestCase
4
4
  class ::Status < Superstore::Base; end
5
- class ::User < Superstore::Base
5
+ class ::Job < Superstore::Base
6
6
  belongs_to :status
7
7
  end
8
8
 
9
9
  test 'class_name' do
10
- assert_equal 'Status', User.new.belongs_to_reflections[:status].class_name
10
+ assert_equal 'Status', Job.new.belongs_to_reflections[:status].class_name
11
11
  end
12
12
  end
@@ -8,6 +8,15 @@ class Superstore::BelongsToTest < Superstore::TestCase
8
8
  string :widget_id
9
9
  belongs_to :widget, class_name: 'Issue'
10
10
 
11
+ string :other_id
12
+ belongs_to :other_issue, class_name: 'Issue', foreign_key: :other_id
13
+
14
+ string :user_id
15
+ belongs_to :user, primary_key: :special_id
16
+
17
+ string :title_issue_id
18
+ belongs_to :title_issue, class_name: 'Issue', primary_key: :title
19
+
11
20
  string :target_id
12
21
  string :target_type
13
22
  belongs_to :target, polymorphic: true
@@ -37,6 +46,39 @@ class Superstore::BelongsToTest < Superstore::TestCase
37
46
  assert_equal issue, record.widget
38
47
  end
39
48
 
49
+ test 'belongs_to with foreign_key' do
50
+ issue = Issue.create
51
+
52
+ record = TestObject.create(other_issue: issue)
53
+
54
+ assert_equal issue, record.other_issue
55
+ assert_equal issue.id, record.other_id
56
+
57
+ record = TestObject.find(record.id)
58
+ assert_equal issue, record.other_issue
59
+ end
60
+
61
+ test 'belongs_to with primary_key for ActiveRecord' do
62
+ special_id = 'special_id'
63
+ user = User.create! special_id: special_id
64
+ record = TestObject.create user: user
65
+
66
+ assert_equal user, record.user
67
+ assert_equal special_id, record.user_id
68
+
69
+ record = TestObject.find(record.id)
70
+ assert_equal user, record.user
71
+ end
72
+
73
+ test 'belongs_to with primary_key raises an error for non-ActiveRecord' do
74
+ issue = Issue.create title: 'title'
75
+
76
+ record = TestObject.new
77
+ record.title_issue_id = issue.title
78
+ assert_raises(ArgumentError) { record.title_issue }
79
+ assert_raises(ArgumentError) { record.title_issue = issue }
80
+ end
81
+
40
82
  test 'belongs_to with polymorphic' do
41
83
  issue = Issue.create
42
84
 
@@ -2,24 +2,20 @@ require 'test_helper'
2
2
 
3
3
  class Superstore::SchemaTest < Superstore::TestCase
4
4
  # SELECT columnfamily_name FROM System.schema_columnfamilies WHERE keyspace_name='myKeyspaceName';
5
- test "create_table" do
6
- Superstore::Schema.create_table 'TestRecords', 'compression_parameters:sstable_compression' => 'SnappyCompressor'
5
+ test "create and drop_table" do
6
+ Superstore::Schema.create_table 'TestRecords'
7
7
 
8
8
  begin
9
9
  Superstore::Schema.create_table 'TestRecords'
10
10
  assert false, 'TestRecords should already exist'
11
11
  rescue Exception => e
12
12
  end
13
- end
14
-
15
- test "drop_table" do
16
- Superstore::Schema.create_table 'TestCFToDrop'
17
13
 
18
- Superstore::Schema.drop_table 'TestCFToDrop'
14
+ Superstore::Schema.drop_table 'TestRecords'
19
15
 
20
16
  begin
21
- Superstore::Schema.drop_table 'TestCFToDrop'
22
- assert false, 'TestCFToDrop should not exist'
17
+ Superstore::Schema.drop_table 'TestRecords'
18
+ assert false, 'TestRecords should not exist'
23
19
  rescue Exception => e
24
20
  end
25
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: superstore
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Koziarski
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-02 00:00:00.000000000 Z
12
+ date: 2014-05-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -106,6 +106,8 @@ files:
106
106
  - test/support/cassandra.rb
107
107
  - test/support/hstore.rb
108
108
  - test/support/issue.rb
109
+ - test/support/pg.rb
110
+ - test/support/user.rb
109
111
  - test/test_helper.rb
110
112
  - test/unit/active_model_test.rb
111
113
  - test/unit/adapters/adapter_test.rb