go-on-rails 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/lib/generators/gor/go-on-rails/association.rb +12 -2
- data/lib/generators/gor/templates/gor_model.go.erb +41 -23
- 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: 890e56dfb579f5a2db328a4ee43084ce7feea0f3
|
4
|
+
data.tar.gz: 6c902e303478da0632649868a56c91f8b00cbeb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f166b780f245457d28dd1ddb96af9d32d5c40f3f447da1ddfd72deecb2290b8a9f7c9304f52772cdf94be2e7b1bb849733dc353807de047127271434dc06ee1b
|
7
|
+
data.tar.gz: cde88337c17f6aff8447835182796a2067c07eb8dfa4c38288bfab5e114fbf72c271c6b61318e1a78732d0072dff0676fc8281ce6978c880f10c5864a5686b87
|
data/README.md
CHANGED
@@ -13,15 +13,15 @@ Here's a simple [example(tutorial)](https://github.com/goonr/example_simple) sho
|
|
13
13
|
|
14
14
|
## Prerequisites
|
15
15
|
|
16
|
-
* Rails
|
17
|
-
* Golang
|
16
|
+
* Rails 4.2+
|
17
|
+
* Golang 1.4+
|
18
18
|
|
19
19
|
## Installation
|
20
20
|
|
21
21
|
Add this line to your application's Gemfile:
|
22
22
|
|
23
23
|
```ruby
|
24
|
-
gem 'go-on-rails', '~> 0.1.
|
24
|
+
gem 'go-on-rails', '~> 0.1.2'
|
25
25
|
```
|
26
26
|
|
27
27
|
And then execute:
|
@@ -15,7 +15,11 @@ module GoOnRails
|
|
15
15
|
col_name = assoc.name.to_s.camelize
|
16
16
|
class_name = assoc.name.to_s.singularize.camelize
|
17
17
|
unless assoc.options.empty?
|
18
|
-
|
18
|
+
if assoc.options.key? :class_name
|
19
|
+
class_name = assoc.options[:class_name]
|
20
|
+
elsif assoc.options.key? :source
|
21
|
+
class_name = assoc.options[:source].to_s.camelize
|
22
|
+
end
|
19
23
|
end
|
20
24
|
type_name = "[]#{class_name}"
|
21
25
|
info[:assoc_info][:has_many][col_name] = {class_name: class_name}
|
@@ -23,7 +27,13 @@ module GoOnRails
|
|
23
27
|
|
24
28
|
when :has_one, :belongs_to
|
25
29
|
col_name = class_name = assoc.name.to_s.camelize
|
26
|
-
|
30
|
+
unless assoc.options.empty?
|
31
|
+
if assoc.options.key? :class_name
|
32
|
+
class_name = assoc.options[:class_name]
|
33
|
+
elsif assoc.options.key? :source
|
34
|
+
class_name = assoc.options[:source].to_s.camelize
|
35
|
+
end
|
36
|
+
end
|
27
37
|
type_name = class_name
|
28
38
|
info[:assoc_info][assoc.macro][col_name] = {class_name: class_name}
|
29
39
|
info[:assoc_info][assoc.macro][col_name].merge!(assoc.options) unless assoc.options.empty?
|
@@ -199,11 +199,11 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
|
|
199
199
|
<%- if v[:through] || v[:as] -%>
|
200
200
|
// FIXME: optimize the query
|
201
201
|
for i, vvv := range _<%= param_name_plural %> {
|
202
|
-
_<%=
|
202
|
+
_<%= k.underscore.pluralize %>, err := <%= @model_name %>Get<%= k.pluralize %>(vvv.Id)
|
203
203
|
if err != nil {
|
204
204
|
continue
|
205
205
|
}
|
206
|
-
vvv.<%= k %> = _<%=
|
206
|
+
vvv.<%= k %> = _<%= k.underscore.pluralize %>
|
207
207
|
_<%= param_name_plural %>[i] = vvv
|
208
208
|
}
|
209
209
|
<%- else -%>
|
@@ -345,10 +345,28 @@ func Find<%= @model_name.pluralize %>Where(where string, args ...interface{}) (<
|
|
345
345
|
return <%= param_name_plural %>, nil
|
346
346
|
}
|
347
347
|
|
348
|
-
// Find<%= @model_name
|
349
|
-
// with placeholders, eg:
|
348
|
+
// Find<%= @model_name %>BySql query use a complete SQL clause
|
349
|
+
// with placeholders, eg: FindUserBySql("SELECT * FROM users WHERE first_name = ? AND age > ? ORDER BY DESC LIMIT 1", "John", 18)
|
350
|
+
// will return only One record in the table "users" whose first_name is "John" and age elder than 18
|
351
|
+
func Find<%= @model_name %>BySql(sql string, args ...interface{}) (*<%= @model_name %>, error) {
|
352
|
+
stmt, err := db.Preparex(db.Rebind(sql))
|
353
|
+
if err != nil {
|
354
|
+
log.Println(err)
|
355
|
+
return nil, err
|
356
|
+
}
|
357
|
+
_<%= model_name_underscore %> := &<%= @model_name %>{}
|
358
|
+
err = stmt.Get(_<%= model_name_underscore %>, args...)
|
359
|
+
if err != nil {
|
360
|
+
log.Println(err)
|
361
|
+
return nil, err
|
362
|
+
}
|
363
|
+
return _<%= model_name_underscore %>, nil
|
364
|
+
}
|
365
|
+
|
366
|
+
// Find<%= @model_name.pluralize %>BySql query use a complete SQL clause
|
367
|
+
// with placeholders, eg: FindUsersBySql("SELECT * FROM users WHERE first_name = ? AND age > ?", "John", 18)
|
350
368
|
// will return those records in the table "users" whose first_name is "John" and age elder than 18
|
351
|
-
func Find<%= @model_name.pluralize %>
|
369
|
+
func Find<%= @model_name.pluralize %>BySql(sql string, args ...interface{}) (<%= param_name_plural %> []<%= @model_name %>, err error) {
|
352
370
|
stmt, err := db.Preparex(db.Rebind(sql))
|
353
371
|
if err != nil {
|
354
372
|
log.Println(err)
|
@@ -428,8 +446,8 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Create() (int64, error)
|
|
428
446
|
<%- unless @struct_info[:assoc_info][:has_many].empty? -%>
|
429
447
|
<%- has_many = @struct_info[:assoc_info][:has_many] -%>
|
430
448
|
<%- has_many.each do |k, v| -%>
|
431
|
-
// <%=
|
432
|
-
func (_<%= model_name_underscore %> *<%= @model_name %>) <%=
|
449
|
+
// <%= k.pluralize %>Create used to create the associated objects
|
450
|
+
func (_<%= model_name_underscore %> *<%= @model_name %>) <%= k.pluralize %>Create(am map[string]interface{}) error {
|
433
451
|
<%- if v[:through] -%>
|
434
452
|
// FIXME: use transaction to create these associated objects
|
435
453
|
<%= v[:class_name].underscore %>Id, err := Create<%= v[:class_name] %>(am)
|
@@ -452,17 +470,17 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) <%= v[:class_name].plur
|
|
452
470
|
return err
|
453
471
|
}
|
454
472
|
|
455
|
-
// Get<%=
|
456
|
-
func (_<%= model_name_underscore %> *<%= @model_name %>) Get<%=
|
457
|
-
_<%=
|
473
|
+
// Get<%= k.pluralize %> used to get associated objects
|
474
|
+
func (_<%= model_name_underscore %> *<%= @model_name %>) Get<%= k.pluralize %>() error {
|
475
|
+
_<%= k.underscore.pluralize %>, err := <%= @model_name %>Get<%= k.pluralize %>(_<%= model_name_underscore %>.Id)
|
458
476
|
if err == nil {
|
459
|
-
_<%= model_name_underscore %>.<%= k %> = _<%=
|
477
|
+
_<%= model_name_underscore %>.<%= k %> = _<%= k.underscore.pluralize %>
|
460
478
|
}
|
461
479
|
return err
|
462
480
|
}
|
463
481
|
|
464
|
-
// <%= @model_name %>Get<%=
|
465
|
-
func <%= @model_name %>Get<%=
|
482
|
+
// <%= @model_name %>Get<%= k.pluralize %> a helper fuction used to get associated objects for <%= @model_name %>IncludesWhere()
|
483
|
+
func <%= @model_name %>Get<%= k.pluralize %>(id int64) ([]<%= v[:class_name] %>, error) {
|
466
484
|
<%- if v[:through] -%>
|
467
485
|
// FIXME: use transaction to create these associated objects
|
468
486
|
<%- target_table = v[:class_name].underscore.pluralize -%>
|
@@ -472,18 +490,18 @@ func <%= @model_name %>Get<%= v[:class_name].pluralize %>(id int64) ([]<%= v[:cl
|
|
472
490
|
INNER JOIN <%= through_table %>
|
473
491
|
ON <%= target_table %>.id = <%= through_table %>.<%= v[:class_name].underscore %>_id
|
474
492
|
WHERE <%= through_table %>.<%= model_name_underscore %>_id = ?`
|
475
|
-
_<%=
|
493
|
+
_<%= k.underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>BySql(sql, id)
|
476
494
|
<%- elsif v[:as] -%>
|
477
495
|
where := `<%= v[:as] %>_type = "<%= @model_name %>" AND <%= v[:as] %>_id = ?`
|
478
|
-
_<%=
|
496
|
+
_<%= k.underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>Where(where, id)
|
479
497
|
<%- else -%>
|
480
498
|
<%- if v[:foreign_key] -%>
|
481
|
-
_<%=
|
499
|
+
_<%= k.underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>By("<%= v[:foreign_key] %>", id)
|
482
500
|
<%- else -%>
|
483
|
-
_<%=
|
501
|
+
_<%= k.underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>By("<%= model_name_underscore %>_id", id)
|
484
502
|
<%- end -%>
|
485
503
|
<%- end -%>
|
486
|
-
return _<%=
|
504
|
+
return _<%= k.underscore.pluralize %>, err
|
487
505
|
}
|
488
506
|
|
489
507
|
<%- end -%>
|
@@ -492,7 +510,7 @@ func <%= @model_name %>Get<%= v[:class_name].pluralize %>(id int64) ([]<%= v[:cl
|
|
492
510
|
<%- unless @struct_info[:assoc_info][:has_one].empty? -%>
|
493
511
|
<%- has_one = @struct_info[:assoc_info][:has_one] -%>
|
494
512
|
<%- has_one.each do |k, v| -%>
|
495
|
-
func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%=
|
513
|
+
func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%= k %>(am map[string]interface{}) error {
|
496
514
|
<%- if v[:foreign_key] -%>
|
497
515
|
am["<%= v[:foreign_key] %>"] = _<%= model_name_underscore %>.Id
|
498
516
|
<%- else -%>
|
@@ -509,7 +527,7 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%= v[:class_name
|
|
509
527
|
<%- belongs_to.each do |k, v| -%>
|
510
528
|
<%-# don't create virtual table for polymorphic model -%>
|
511
529
|
<%- next if v[:polymorphic] -%>
|
512
|
-
func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%=
|
530
|
+
func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%= k %>(am map[string]interface{}) error {
|
513
531
|
<%- if v[:foreign_key] -%>
|
514
532
|
am["<%= v[:foreign_key] %>"] = _<%= model_name_underscore %>.Id
|
515
533
|
<%- else -%>
|
@@ -655,7 +673,7 @@ func destroy<%= @model_name %>Associations(ids ...int64) {
|
|
655
673
|
// no sql.NULLType supported, just set the associated field to zero value of int64
|
656
674
|
<%- if opts[:through] -%>
|
657
675
|
sql := fmt.Sprintf("UPDATE <%= opts[:through].to_s %> SET <%= opts[:class_name].underscore %>_id = 0 WHERE <%= opts[:class_name].underscore %>_id IN (?%s)", idsHolder)
|
658
|
-
_, err = Update<%= opts[:through].to_s.camelize %>
|
676
|
+
_, err = Update<%= opts[:through].to_s.camelize %>BySql(sql, idsT...)
|
659
677
|
if err != nil {
|
660
678
|
log.Printf("Delete associated object %s error: %v\n", "<%= opts[:class_name].pluralize %>", err)
|
661
679
|
}
|
@@ -665,7 +683,7 @@ func destroy<%= @model_name %>Associations(ids ...int64) {
|
|
665
683
|
<%- else -%>
|
666
684
|
sql := fmt.Sprintf("UPDATE <%= opts[:class_name].underscore.pluralize %> SET <%= model_name_underscore %>_id = 0 WHERE <%= model_name_underscore %>_id IN (?%s)", idsHolder)
|
667
685
|
<%- end -%>
|
668
|
-
_, err = Update<%= opts[:class_name].pluralize %>
|
686
|
+
_, err = Update<%= opts[:class_name].pluralize %>BySql(sql, idsT...)
|
669
687
|
if err != nil {
|
670
688
|
log.Printf("Delete associated object %s error: %v\n", "<%= opts[:class_name].pluralize %>", err)
|
671
689
|
}
|
@@ -753,7 +771,7 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) UpdateColumns(am map[st
|
|
753
771
|
return err
|
754
772
|
}
|
755
773
|
|
756
|
-
func Update<%= @model_name.pluralize %>
|
774
|
+
func Update<%= @model_name.pluralize %>BySql(sql string, args ...interface{}) (int64, error) {
|
757
775
|
if sql == "" {
|
758
776
|
return 0, errors.New("A blank SQL clause")
|
759
777
|
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: go-on-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- B1nj0y
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Modeling, developing and testing your Golang app with your familiar Rails
|
14
14
|
tools like rails generate, db migration, console etc. It is more meant to help integrating
|