findable 0.1.5 → 0.2.0
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.
- checksums.yaml +4 -4
- data/lib/findable.rb +1 -0
- data/lib/findable/associations/active_record_ext.rb +9 -0
- data/lib/findable/base.rb +24 -40
- data/lib/findable/collection.rb +94 -0
- data/lib/findable/errors.rb +6 -0
- data/lib/findable/schema.rb +37 -0
- data/lib/findable/schema/conversion.rb +74 -0
- data/lib/findable/version.rb +1 -1
- data/spec/findable/associations/active_record_ext_spec.rb +16 -13
- data/spec/findable/associations_spec.rb +16 -15
- data/spec/findable/base_spec.rb +6 -6
- data/spec/findable/query_spec.rb +4 -4
- data/spec/spec_helper.rb +4 -0
- data/spec/support/initialize_database.rb +53 -42
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 022bdcf5a11ab55395af5ca7af256b23afd96376
|
4
|
+
data.tar.gz: 86900a51201b84491a1e361f0945ec4d36b4e064
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 880d6023b382295648fd89c61c4e1bba1a73bc3a6e9266b7b84840d0eee4722ef388a8e94ba9b5c166e752f74b79a36b24577b1f6ba68998e5726306a1fd385d
|
7
|
+
data.tar.gz: 0670803e036bd49527ebc2d4208afb298ab5faed78eee5e2fc8052a00b340b4228efc7b6fbec58bc63d9e890aa7902b516b09afc9c52bd9208969de6437be339
|
data/lib/findable.rb
CHANGED
@@ -57,6 +57,15 @@ module Findable
|
|
57
57
|
Utils.add_reflection(:belongs_to, name.to_sym, options, self)
|
58
58
|
else
|
59
59
|
super
|
60
|
+
define_method("#{name}_with_findable_polymorphic") do
|
61
|
+
begin
|
62
|
+
public_send("#{name}_without_findable_polymorphic")
|
63
|
+
rescue NotActiveRecord
|
64
|
+
id = public_send("#{name}_id")
|
65
|
+
public_send("#{name}_type").constantize.find(id)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
alias_method_chain name.to_sym, :findable_polymorphic
|
60
69
|
end
|
61
70
|
end
|
62
71
|
end
|
data/lib/findable/base.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "findable/associations"
|
2
|
+
require "findable/schema"
|
2
3
|
require "findable/inspection"
|
3
4
|
|
4
5
|
module Findable
|
@@ -6,39 +7,32 @@ module Findable
|
|
6
7
|
include ActiveModel::Model
|
7
8
|
include ActiveModel::AttributeMethods
|
8
9
|
include Associations
|
10
|
+
include Schema
|
9
11
|
include Inspection
|
10
12
|
|
11
|
-
attribute_method_suffix "="
|
12
|
-
attribute_method_suffix "?"
|
13
|
-
|
14
13
|
class << self
|
15
|
-
|
16
|
-
|
17
|
-
def define_field(attr)
|
18
|
-
unless public_method_defined?(attr)
|
19
|
-
define_attribute_methods attr
|
20
|
-
define_method(attr) { attributes[attr] }
|
21
|
-
column_names << attr.to_sym
|
22
|
-
end
|
14
|
+
def arel_table
|
15
|
+
raise NotActiveRecord.new(self)
|
23
16
|
end
|
24
17
|
|
25
18
|
## ActiveRecord like APIs
|
26
19
|
|
27
|
-
delegate :
|
20
|
+
delegate :first, :last, :order, :pluck, to: :all
|
21
|
+
alias_method :take, :first
|
28
22
|
|
29
23
|
def primary_key
|
30
24
|
"id"
|
31
25
|
end
|
32
26
|
|
33
|
-
def
|
34
|
-
|
27
|
+
def all
|
28
|
+
collection!(query.all)
|
35
29
|
end
|
36
30
|
|
37
31
|
def find(ids)
|
38
32
|
if records = find_by_ids(ids).presence
|
39
|
-
ids.is_a?(Array) ? records : records.first
|
33
|
+
ids.is_a?(Array) ? collection!(records) : records.first
|
40
34
|
else
|
41
|
-
raise
|
35
|
+
raise not_found(id: ids)
|
42
36
|
end
|
43
37
|
end
|
44
38
|
|
@@ -56,9 +50,7 @@ module Findable
|
|
56
50
|
}
|
57
51
|
end
|
58
52
|
else
|
59
|
-
all.
|
60
|
-
conditions.all? {|k, v| record.public_send(k) == v }
|
61
|
-
}
|
53
|
+
all.find_by(conditions.dup)
|
62
54
|
end
|
63
55
|
else
|
64
56
|
find_by_ids(conditions).first
|
@@ -66,7 +58,7 @@ module Findable
|
|
66
58
|
end
|
67
59
|
|
68
60
|
def find_by!(conditions)
|
69
|
-
find_by(conditions.dup) || (raise
|
61
|
+
find_by(conditions.dup) || (raise not_found(conditions))
|
70
62
|
end
|
71
63
|
|
72
64
|
def where(conditions)
|
@@ -74,16 +66,14 @@ module Findable
|
|
74
66
|
if id = conditions.delete(:id)
|
75
67
|
records = find_by_ids(id)
|
76
68
|
if conditions.empty?
|
77
|
-
records
|
69
|
+
collection!(records)
|
78
70
|
else
|
79
|
-
records.select {|record|
|
71
|
+
collection!(records.select {|record|
|
80
72
|
conditions.all? {|k, v| record.public_send(k) == v }
|
81
|
-
}
|
73
|
+
})
|
82
74
|
end
|
83
75
|
else
|
84
|
-
all.
|
85
|
-
conditions.all? {|k, v| record.public_send(k) == v }
|
86
|
-
}
|
76
|
+
all.where(conditions.dup)
|
87
77
|
end
|
88
78
|
end
|
89
79
|
|
@@ -94,12 +84,6 @@ module Findable
|
|
94
84
|
end
|
95
85
|
alias_method :create!, :create
|
96
86
|
|
97
|
-
[:first, :last].each do |m|
|
98
|
-
define_method(m) do
|
99
|
-
self.all.public_send(m)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
87
|
## Query APIs
|
104
88
|
|
105
89
|
delegate :find_by_ids, :insert, to: :query
|
@@ -125,6 +109,14 @@ module Findable
|
|
125
109
|
end
|
126
110
|
|
127
111
|
private
|
112
|
+
def collection!(records)
|
113
|
+
records.is_a?(Array) ? Collection.new(self, records) : records
|
114
|
+
end
|
115
|
+
|
116
|
+
def not_found(params)
|
117
|
+
RecordNotFound.new(self, params)
|
118
|
+
end
|
119
|
+
|
128
120
|
def id_from(obj)
|
129
121
|
obj.is_a?(self) ? obj.id : obj.to_i
|
130
122
|
end
|
@@ -169,13 +161,5 @@ module Findable
|
|
169
161
|
def attributes
|
170
162
|
@_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
|
171
163
|
end
|
172
|
-
|
173
|
-
def attribute=(attr, value)
|
174
|
-
attributes[attr.to_sym] = value
|
175
|
-
end
|
176
|
-
|
177
|
-
def attribute?(attr)
|
178
|
-
attributes[attr.to_sym].present?
|
179
|
-
end
|
180
164
|
end
|
181
165
|
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module Findable
|
2
|
+
# Record collection class
|
3
|
+
class Collection
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
attr_reader :model, :records
|
7
|
+
|
8
|
+
def initialize(model, records)
|
9
|
+
raise ArgumentError unless records.is_a?(Array)
|
10
|
+
@model = model
|
11
|
+
@records = records
|
12
|
+
end
|
13
|
+
|
14
|
+
delegate :size, :first, :last, to: :records
|
15
|
+
delegate :empty?, :blank?, :present?, to: :records
|
16
|
+
alias_method :length, :size
|
17
|
+
alias_method :count, :size
|
18
|
+
alias_method :take, :first
|
19
|
+
alias_method :to_a, :records
|
20
|
+
|
21
|
+
def presence
|
22
|
+
present? ? self : nil
|
23
|
+
end
|
24
|
+
|
25
|
+
def each
|
26
|
+
if block_given?
|
27
|
+
records.each {|record| yield(record) }
|
28
|
+
else
|
29
|
+
records.to_enum
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def find(ids)
|
34
|
+
if ids.is_a?(Array)
|
35
|
+
if refined = records.select {|record| record.id.in?(ids) }
|
36
|
+
regenerate(refined)
|
37
|
+
else
|
38
|
+
raise not_found(id: ids)
|
39
|
+
end
|
40
|
+
else
|
41
|
+
records.detect {|record| record.id == ids } || (raise not_found(id: ids))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def find_by(conditions)
|
46
|
+
records.detect {|record|
|
47
|
+
conditions.all? {|k, v| record.public_send(k) == v }
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
def find_by!(conditions)
|
52
|
+
find_by(conditions.dup) || (raise not_found(conditions))
|
53
|
+
end
|
54
|
+
|
55
|
+
def where(conditions)
|
56
|
+
regenerate(records.select {|record|
|
57
|
+
conditions.all? {|k, v| record.public_send(k) == v }
|
58
|
+
})
|
59
|
+
end
|
60
|
+
|
61
|
+
def order(*columns)
|
62
|
+
columns.flatten!
|
63
|
+
raise ArgumentError, "Must contain arguments" if columns.empty?
|
64
|
+
|
65
|
+
regenerate(records.sort_by {|record|
|
66
|
+
columns.map {|column| record.public_send(column) }
|
67
|
+
})
|
68
|
+
end
|
69
|
+
|
70
|
+
def pluck(*columns)
|
71
|
+
columns.flatten!
|
72
|
+
return records.map {|record| record.attributes.values } if columns.empty?
|
73
|
+
single = (columns.size == 1)
|
74
|
+
|
75
|
+
records.map {|record|
|
76
|
+
values = columns.map {|column| record.public_send(column) }
|
77
|
+
single ? values.first : values
|
78
|
+
}
|
79
|
+
end
|
80
|
+
|
81
|
+
def inspect
|
82
|
+
"[#{records.map(&:inspect).join(",\n")}]"
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
def regenerate(records)
|
87
|
+
self.class.new(model, records)
|
88
|
+
end
|
89
|
+
|
90
|
+
def not_found(params)
|
91
|
+
RecordNotFound.new(model, params)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
data/lib/findable/errors.rb
CHANGED
@@ -0,0 +1,37 @@
|
|
1
|
+
require "findable/schema/conversion"
|
2
|
+
|
3
|
+
module Findable
|
4
|
+
module Schema
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
attribute_method_suffix "="
|
9
|
+
attribute_method_suffix "?"
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def column_names
|
14
|
+
@_column_names ||= [:id]
|
15
|
+
end
|
16
|
+
|
17
|
+
def define_field(*args)
|
18
|
+
options = args.extract_options!
|
19
|
+
name = args.first
|
20
|
+
if !public_method_defined?(name) || options.present?
|
21
|
+
define_attribute_methods name
|
22
|
+
conversion = Conversion.for(options[:type])
|
23
|
+
define_method(name) { conversion.call(attributes[name.to_sym]) }
|
24
|
+
column_names << name.to_sym
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def attribute=(attr, value)
|
30
|
+
attributes[attr] = value
|
31
|
+
end
|
32
|
+
|
33
|
+
def attribute?(attr)
|
34
|
+
attributes[attr].present?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Findable
|
2
|
+
module Schema
|
3
|
+
class Conversion
|
4
|
+
class << self
|
5
|
+
FALSE_VALUE = ["false", "0"]
|
6
|
+
|
7
|
+
def for(type)
|
8
|
+
return types[:default] if type.nil?
|
9
|
+
types[type] || add_type!(type)
|
10
|
+
end
|
11
|
+
|
12
|
+
def types
|
13
|
+
@_types ||= {
|
14
|
+
default: Proc.new {|value| value }
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def add_type!(type)
|
19
|
+
return type if type.respond_to?(:call)
|
20
|
+
raise ArgumentError unless private_method_defined?(type)
|
21
|
+
types[type.to_sym] = method(type)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
# Conversion methods
|
26
|
+
def integer(value)
|
27
|
+
value.to_i
|
28
|
+
end
|
29
|
+
|
30
|
+
def float(value)
|
31
|
+
value.to_f
|
32
|
+
end
|
33
|
+
|
34
|
+
def decimal(value)
|
35
|
+
BigDecimal(value)
|
36
|
+
end
|
37
|
+
|
38
|
+
def string(value)
|
39
|
+
value.to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
def boolean(value)
|
43
|
+
if value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
44
|
+
value
|
45
|
+
elsif value.in?(FALSE_VALUE)
|
46
|
+
false
|
47
|
+
else
|
48
|
+
!!value
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def date(value)
|
53
|
+
return value if value.is_a?(Date)
|
54
|
+
Date.parse(value)
|
55
|
+
end
|
56
|
+
|
57
|
+
def datetime(value)
|
58
|
+
if value.is_a?(Time) || value.is_a?(ActiveSupport::TimeWithZone)
|
59
|
+
return value
|
60
|
+
end
|
61
|
+
Time.zone.parse(value)
|
62
|
+
end
|
63
|
+
|
64
|
+
def symbol(value)
|
65
|
+
value.to_sym
|
66
|
+
end
|
67
|
+
|
68
|
+
def inquiry(value)
|
69
|
+
value.to_s.inquiry
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/findable/version.rb
CHANGED
@@ -1,29 +1,32 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe Findable::Associations::ActiveRecordExt do
|
4
|
-
|
5
|
-
let(:store) { Store.first }
|
6
|
-
let(:email) { Email.first }
|
4
|
+
# ActiveRecord models
|
7
5
|
let(:user) { User.first }
|
6
|
+
let(:user2) { User.second }
|
7
|
+
let(:status) { Status.take }
|
8
|
+
|
9
|
+
# Findable models
|
10
|
+
let(:image) { Image.take }
|
8
11
|
|
9
12
|
describe "#has_many" do
|
10
|
-
it { expect(
|
11
|
-
it { expect(
|
12
|
-
it { expect(
|
13
|
-
it { expect(
|
13
|
+
it { expect(user.purchase_histories).to be_kind_of(Findable::Collection) }
|
14
|
+
it { expect(user.purchase_histories.first).to be_kind_of(PurchaseHistory) }
|
15
|
+
it { expect(user.comments).to be_kind_of(ActiveRecord::Relation) }
|
16
|
+
it { expect(user.comments.first).to be_kind_of(Comment) }
|
14
17
|
end
|
15
18
|
|
16
19
|
describe "#has_one" do
|
17
|
-
it { expect(
|
18
|
-
it { expect(
|
20
|
+
it { expect(user.image).to be_kind_of(Image) }
|
21
|
+
it { expect(user.status).to be_kind_of(Status) }
|
19
22
|
end
|
20
23
|
|
21
24
|
describe "#belongs_to" do
|
22
|
-
it { expect(
|
23
|
-
it { expect(
|
25
|
+
it { expect(image.user).to be_kind_of(User) }
|
26
|
+
it { expect(status.user).to be_kind_of(User) }
|
24
27
|
it {
|
25
|
-
|
26
|
-
expect(
|
28
|
+
image.user = user2
|
29
|
+
expect(image).to have_attributes(user_id: user2.id)
|
27
30
|
}
|
28
31
|
end
|
29
32
|
end
|
@@ -1,33 +1,34 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe Findable::Associations do
|
4
|
-
|
5
|
-
let(:
|
6
|
-
let(:
|
7
|
-
let(:
|
8
|
-
let(:
|
9
|
-
let(:
|
4
|
+
# Findable models
|
5
|
+
let(:category) { Category.find(CategoryData.first[:id]) }
|
6
|
+
let(:category2) { Category.find(CategoryData.second[:id]) }
|
7
|
+
let(:product) { Product.take }
|
8
|
+
let(:image) { Image.find(ImageData.first[:id]) }
|
9
|
+
let(:image2) { Image.find(ImageData.second[:id]) }
|
10
|
+
let(:cart) { Cart.take }
|
10
11
|
|
11
12
|
describe "#has_many" do
|
12
|
-
it { expect(category.products).to be_kind_of(
|
13
|
+
it { expect(category.products).to be_kind_of(Findable::Collection) }
|
13
14
|
it { expect(category.products.first).to be_kind_of(Product) }
|
14
|
-
it { expect(
|
15
|
-
it { expect(
|
15
|
+
it { expect(product.comments).to be_kind_of(ActiveRecord::Relation) }
|
16
|
+
it { expect(product.comments.first).to be_kind_of(Comment) }
|
16
17
|
end
|
17
18
|
|
18
19
|
describe "#has_one" do
|
19
20
|
it { expect(product.image).to be_kind_of(Image) }
|
20
|
-
it { expect(user
|
21
|
+
it { expect(cart.user).to be_kind_of(User) }
|
21
22
|
end
|
22
23
|
|
23
24
|
describe "#belongs_to" do
|
24
25
|
it { expect(product.category).to be_kind_of(Category) }
|
25
|
-
it { expect(user
|
26
|
-
it { expect(
|
27
|
-
it { expect(
|
26
|
+
it { expect(image.user).to be_kind_of(User) }
|
27
|
+
it { expect(image.content).to be_kind_of(image.content_type.constantize) }
|
28
|
+
it { expect(image2.content).to be_kind_of(image2.content_type.constantize) }
|
28
29
|
it {
|
29
|
-
|
30
|
-
expect(
|
30
|
+
product.category = category2
|
31
|
+
expect(product).to have_attributes(category_id: category2.id)
|
31
32
|
}
|
32
33
|
end
|
33
34
|
end
|
data/spec/findable/base_spec.rb
CHANGED
@@ -13,13 +13,13 @@ describe Findable::Base do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
describe ".all" do
|
16
|
-
it { expect(read_model.all).to be_kind_of(
|
17
|
-
it { expect(read_model.all.size).to eq(
|
16
|
+
it { expect(read_model.all).to be_kind_of(Findable::Collection) }
|
17
|
+
it { expect(read_model.all.size).to eq(CategoryData.size) }
|
18
18
|
end
|
19
19
|
|
20
20
|
describe ".find" do
|
21
21
|
it { expect(read_model.find(id)).to be_kind_of(read_model) }
|
22
|
-
it { expect(read_model.find([id])).to be_kind_of(
|
22
|
+
it { expect(read_model.find([id])).to be_kind_of(Findable::Collection) }
|
23
23
|
it {
|
24
24
|
expect {
|
25
25
|
read_model.find(invalid_id)
|
@@ -52,13 +52,13 @@ describe Findable::Base do
|
|
52
52
|
end
|
53
53
|
|
54
54
|
describe ".where" do
|
55
|
-
it { expect(read_model.where(id: id)).to be_kind_of(
|
55
|
+
it { expect(read_model.where(id: id)).to be_kind_of(Findable::Collection) }
|
56
56
|
it { expect(read_model.where(id: id).first).to be_kind_of(read_model) }
|
57
57
|
it { expect(read_model.where(id: invalid_id)).to be_empty }
|
58
|
-
it { expect(read_model.where(id: id, name: name)).to be_kind_of(
|
58
|
+
it { expect(read_model.where(id: id, name: name)).to be_kind_of(Findable::Collection) }
|
59
59
|
it { expect(read_model.where(id: id, name: name).first).to be_kind_of(read_model) }
|
60
60
|
it { expect(read_model.where(id: invalid_id, name: name)).to be_empty }
|
61
|
-
it { expect(read_model.where(name: name)).to be_kind_of(
|
61
|
+
it { expect(read_model.where(name: name)).to be_kind_of(Findable::Collection) }
|
62
62
|
it { expect(read_model.where(name: name).first).to be_kind_of(read_model) }
|
63
63
|
it { expect(read_model.where(name: invalid_name)).to be_empty }
|
64
64
|
end
|
data/spec/findable/query_spec.rb
CHANGED
@@ -8,16 +8,16 @@ describe Findable::Query do
|
|
8
8
|
|
9
9
|
describe "#data" do
|
10
10
|
it { expect(read_model.query.all).to be_kind_of(Array) }
|
11
|
-
it { expect(read_model.query.all.size).to eq(
|
11
|
+
it { expect(read_model.query.all.size).to eq(CategoryData.size) }
|
12
12
|
end
|
13
13
|
|
14
14
|
describe "#ids" do
|
15
15
|
it { expect(read_model.query.ids).to be_kind_of(Array) }
|
16
|
-
it { expect(read_model.query.ids).to eq([
|
16
|
+
it { expect(read_model.query.ids).to eq(CategoryData.map {|h| h[:id]}) }
|
17
17
|
end
|
18
18
|
|
19
19
|
describe "#count" do
|
20
|
-
it { expect(read_model.query.count).to eq(
|
20
|
+
it { expect(read_model.query.count).to eq(CategoryData.size) }
|
21
21
|
end
|
22
22
|
|
23
23
|
describe "#find_by_ids" do
|
@@ -30,7 +30,7 @@ describe Findable::Query do
|
|
30
30
|
|
31
31
|
describe "#exists?" do
|
32
32
|
it { expect(read_model.exists?(1)).to be_truthy }
|
33
|
-
it { expect(read_model.exists?(
|
33
|
+
it { expect(read_model.exists?(100)).to be_falsey }
|
34
34
|
end
|
35
35
|
|
36
36
|
describe "#insert" do
|
data/spec/spec_helper.rb
CHANGED
@@ -20,6 +20,10 @@ require "pry"
|
|
20
20
|
require "findable"
|
21
21
|
require "findable/associations/active_record_ext"
|
22
22
|
|
23
|
+
Findable.configure do |config|
|
24
|
+
config.redis_options = { host: "localhost", port: 6379, db: 10 }
|
25
|
+
end
|
26
|
+
|
23
27
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
24
28
|
|
25
29
|
RSpec.configure do |config|
|
@@ -3,7 +3,7 @@
|
|
3
3
|
# =========================
|
4
4
|
|
5
5
|
# Initialize Redis
|
6
|
-
|
6
|
+
Findable::Base.query.redis.flushdb
|
7
7
|
|
8
8
|
# Initialize SQLite
|
9
9
|
ActiveRecord::Base.establish_connection({
|
@@ -11,24 +11,26 @@ ActiveRecord::Base.establish_connection({
|
|
11
11
|
database: ":memory:",
|
12
12
|
})
|
13
13
|
|
14
|
-
ActiveRecord::Migration.create_table :
|
14
|
+
ActiveRecord::Migration.create_table :users do |t|
|
15
15
|
t.string :name
|
16
|
+
t.integer :cart_id
|
17
|
+
t.string :content_type
|
18
|
+
t.integer :content_id
|
16
19
|
end
|
17
20
|
|
18
|
-
ActiveRecord::Migration.create_table :
|
19
|
-
t.
|
20
|
-
t.integer :
|
21
|
+
ActiveRecord::Migration.create_table :statuses do |t|
|
22
|
+
t.integer :total_payments
|
23
|
+
t.integer :user_id
|
21
24
|
end
|
22
25
|
|
23
|
-
ActiveRecord::Migration.create_table :
|
24
|
-
t.
|
25
|
-
t.integer :store_id
|
26
|
+
ActiveRecord::Migration.create_table :comments do |t|
|
27
|
+
t.text :body
|
26
28
|
t.integer :user_id
|
29
|
+
t.integer :product_id
|
27
30
|
end
|
28
31
|
|
29
32
|
ActiveRecord::Migration.create_table :pictures do |t|
|
30
33
|
t.string :name
|
31
|
-
t.integer :user_id
|
32
34
|
end
|
33
35
|
|
34
36
|
# =========================
|
@@ -36,12 +38,12 @@ end
|
|
36
38
|
# =========================
|
37
39
|
|
38
40
|
# Model < Findable
|
39
|
-
%w(Category Product Image
|
41
|
+
%w(Category Product Image PurchaseHistory Cart).each do |class_name|
|
40
42
|
Object.const_set(class_name, Class.new(Findable::Base))
|
41
43
|
end
|
42
44
|
|
43
45
|
# Model < ActiveRecord
|
44
|
-
%w(
|
46
|
+
%w(User Comment Status Picture).each do |class_name|
|
45
47
|
Object.const_set(class_name, Class.new(ActiveRecord::Base))
|
46
48
|
end
|
47
49
|
|
@@ -49,58 +51,67 @@ ActiveRecord::Base.subclasses.each do |ar|
|
|
49
51
|
ar.include Findable::Associations::ActiveRecordExt
|
50
52
|
end
|
51
53
|
|
52
|
-
# Associations
|
54
|
+
# Associations (Findable <=> Findable)
|
53
55
|
Category.has_many :products
|
54
56
|
Product.belongs_to :category
|
55
57
|
Product.has_one :image
|
56
58
|
Image.belongs_to :product
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
# Associations (ActiveRecord <=> ActiveRecord)
|
61
|
+
User.has_many :comments
|
62
|
+
User.has_one :status
|
63
|
+
Comment.belongs_to :user
|
64
|
+
Status.belongs_to :user
|
62
65
|
|
63
|
-
|
66
|
+
# Associations (Findable <=> ActiveRecord)
|
67
|
+
Product.has_many :comments
|
68
|
+
Comment.belongs_to :product
|
64
69
|
|
65
|
-
|
66
|
-
|
70
|
+
Cart.has_one :user
|
71
|
+
User.belongs_to :cart
|
67
72
|
|
68
|
-
User.
|
69
|
-
|
73
|
+
User.has_many :purchase_histories
|
74
|
+
PurchaseHistory.belongs_to :user
|
70
75
|
|
71
|
-
|
72
|
-
|
76
|
+
User.has_one :image
|
77
|
+
Image.belongs_to :user
|
73
78
|
|
74
|
-
|
75
|
-
|
79
|
+
# Polymorphic association
|
80
|
+
User.belongs_to :content, polymorphic: true
|
81
|
+
Image.belongs_to :content, polymorphic: true
|
76
82
|
|
77
83
|
# =========================
|
78
84
|
# Data import
|
79
85
|
# =========================
|
80
|
-
CategoryData = [
|
86
|
+
CategoryData = [
|
87
|
+
{id: 1, name: "Book"},
|
88
|
+
{id: 2, name: "Computer"},
|
89
|
+
]
|
81
90
|
Category.query.import(CategoryData)
|
82
91
|
|
83
92
|
ProductData = [
|
84
|
-
{id: 1, name: "
|
85
|
-
{id: 2, name: "
|
93
|
+
{id: 1, name: "book1", category_id: 1},
|
94
|
+
{id: 2, name: "book2", category_id: 1},
|
86
95
|
]
|
87
96
|
Product.query.import(ProductData)
|
88
97
|
|
89
98
|
ImageData = [
|
90
|
-
{id: 1, product_id: 1,
|
91
|
-
{id: 2, product_id: 2,
|
99
|
+
{id: 1, product_id: 1, user_id: 1, content_type: "User", content_id: 1},
|
100
|
+
{id: 2, product_id: 2, user_id: 2, content_type: "Category", content_id: 1},
|
92
101
|
]
|
93
102
|
Image.query.import(ImageData)
|
94
103
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
User.
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
104
|
+
CartData = [{id: 1}, {id: 2}]
|
105
|
+
Cart.query.import(CartData)
|
106
|
+
|
107
|
+
Picture.create(name: "example.jpg")
|
108
|
+
User.create(name: "user1", content_type: "Picture", content_id: Picture.first.id, cart_id: CartData.first[:id])
|
109
|
+
User.create(name: "user2", content_type: "Image", content_id: Image.first.id, cart_id: CartData.second[:id])
|
110
|
+
User.all.each do |user|
|
111
|
+
user.create_status!(total_payments: rand(100_000))
|
112
|
+
user.comments.create!(body: "some comment", product_id: Product.take.id)
|
113
|
+
end
|
114
|
+
|
115
|
+
User.all.each do |user|
|
116
|
+
PurchaseHistory.create(user_id: user.id)
|
117
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: findable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- i2bskn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -185,6 +185,7 @@ files:
|
|
185
185
|
- lib/findable/associations/active_record_ext.rb
|
186
186
|
- lib/findable/associations/utils.rb
|
187
187
|
- lib/findable/base.rb
|
188
|
+
- lib/findable/collection.rb
|
188
189
|
- lib/findable/configuration.rb
|
189
190
|
- lib/findable/errors.rb
|
190
191
|
- lib/findable/inspection.rb
|
@@ -194,6 +195,8 @@ files:
|
|
194
195
|
- lib/findable/query/namespace.rb
|
195
196
|
- lib/findable/query/serializer.rb
|
196
197
|
- lib/findable/railtie.rb
|
198
|
+
- lib/findable/schema.rb
|
199
|
+
- lib/findable/schema/conversion.rb
|
197
200
|
- lib/findable/seed.rb
|
198
201
|
- lib/findable/version.rb
|
199
202
|
- lib/generators/findable/install_generator.rb
|