flatten_record 1.0.3 → 1.0.4
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/README.md +44 -12
- data/lib/flatten_record/meta/compute_column.rb +2 -9
- data/lib/flatten_record/version.rb +1 -1
- data/spec/dummy/log/test.log +964 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca6514cd5417800256fc9d02679ebfc487b9434d
|
4
|
+
data.tar.gz: dd8e7587673dd14b19d7692844c5af44617ef090
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 92f296a7b05159983e97ec7c8ed736eec0533aeabd9a0051bba644bf780cebeeed140d208d7d3f24352d26034e15f3fa0cd465a2606fcbc3c8157ea4d8ac1e1d
|
7
|
+
data.tar.gz: 178e1bb147d516a3adcbf46508badc083642ace332338a6bb7942debc3e8646352a8b94646654177f851fa6fb72849a1c1a3440b207f984b02ad3d0a096b8430
|
data/README.md
CHANGED
@@ -1,8 +1,35 @@
|
|
1
1
|
# FlattenRecord [](https://codeclimate.com/github/alvinsj/flatten_record) [](https://travis-ci.org/alvinsj/flatten_record)
|
2
2
|
|
3
|
-
An ActiveRecord plugin that
|
3
|
+
An ActiveRecord plugin that helps to denormalize your existing ActiveRecord models.
|
4
4
|
|
5
|
-
It provides an easier way to
|
5
|
+
It provides an easier way to denormalize/flatten normalized records, and generation the table schema.
|
6
|
+
|
7
|
+
## Example
|
8
|
+
|
9
|
+
Existing normalized tables
|
10
|
+
|
11
|
+
| orders | customers | line_items |
|
12
|
+
|:-------|:---------|:-----------|
|
13
|
+
| id | id | id |
|
14
|
+
| customer_id | name | description |
|
15
|
+
| discount | | total |
|
16
|
+
| total | | order_id |
|
17
|
+
|
18
|
+
Denormalized table, generated by flatten_record
|
19
|
+
|
20
|
+
|denormalized_orders|
|
21
|
+
|:-------------------|
|
22
|
+
| id |
|
23
|
+
| order_id |
|
24
|
+
| discount |
|
25
|
+
| total |
|
26
|
+
| customer_id |
|
27
|
+
| customer_name |
|
28
|
+
| line_item_id |
|
29
|
+
| line_item_description |
|
30
|
+
| line_item_total |
|
31
|
+
| line_items_sum (custom column) |
|
32
|
+
| total_in_usd (custom column) |
|
6
33
|
|
7
34
|
## Usage
|
8
35
|
|
@@ -19,19 +46,24 @@ Include module in your newly defined model
|
|
19
46
|
include FlattenRecord::Flattener
|
20
47
|
|
21
48
|
denormalize :order, {
|
49
|
+
|
50
|
+
# specifying association
|
22
51
|
include: {
|
23
|
-
|
52
|
+
# :belongs_to association
|
24
53
|
customer: {}
|
25
54
|
|
26
55
|
# :has_many association, create multiple denormalized records
|
27
56
|
line_items: {}
|
28
|
-
|
29
|
-
|
30
|
-
|
57
|
+
|
58
|
+
},
|
59
|
+
|
60
|
+
# save results of methods defined in Normalized model
|
61
|
+
methods: {
|
31
62
|
total_in_usd: :decimal
|
32
63
|
},
|
64
|
+
|
65
|
+
# compute results of methods defined in Denormalized model
|
33
66
|
compute: {
|
34
|
-
# compute methods defined in Denormalized model
|
35
67
|
line_items_sum: { type: :decimal, default: 0 }
|
36
68
|
}
|
37
69
|
}
|
@@ -40,9 +72,9 @@ Include module in your newly defined model
|
|
40
72
|
def compute_line_items_sum(order)
|
41
73
|
order.line_items.collect(&:total).inject(:+)
|
42
74
|
end
|
43
|
-
|
75
|
+
end
|
44
76
|
|
45
|
-
|
77
|
+
class Order < ActiveRecord::Base
|
46
78
|
def total_in_usd
|
47
79
|
# calculation
|
48
80
|
end
|
@@ -58,8 +90,8 @@ Update definition and generate new migration file
|
|
58
90
|
$ rails generate flatten_record:migration denormalized_order
|
59
91
|
Warning. Table already exists: denormalized_orders
|
60
92
|
Generating migration based on the difference..
|
61
|
-
Add columns:
|
62
|
-
create db/migrate/
|
93
|
+
Add columns: line_items_description
|
94
|
+
create db/migrate/20140313034736_add_line_items_description_to_denormalized_orders.rb
|
63
95
|
|
64
96
|
### Use denormalizer methods
|
65
97
|
Create record
|
@@ -74,7 +106,7 @@ Update record(s)
|
|
74
106
|
|
75
107
|
irb(main)> DenormalizedOrder.update_with(order)
|
76
108
|
|
77
|
-
##
|
109
|
+
## API Documentation/Development/Discussion
|
78
110
|
|
79
111
|
Refer to the [wiki](https://github.com/alvinsj/flatten_record/wiki).
|
80
112
|
|
@@ -10,16 +10,9 @@ module FlattenRecord
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def denormalize(instance, to_record)
|
13
|
-
|
14
|
-
|
15
|
-
begin
|
16
|
-
to_record = assign_value(to_record, name) do |record|
|
17
|
-
record.send("compute_#{@column.name}".to_sym, instance)
|
18
|
-
end
|
19
|
-
rescue
|
20
|
-
raise "compute_#{@column.name} is not found in #{to_record.inspect}"
|
13
|
+
assign_value(to_record, name) do |record|
|
14
|
+
record.send("compute_#{@column.name}".to_sym, instance)
|
21
15
|
end
|
22
|
-
to_record
|
23
16
|
end
|
24
17
|
end
|
25
18
|
end
|