go-on-rails 0.0.4 → 0.0.5

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: b708a7ebbdda649c91023ff597f4c2572bf2a3ca
4
- data.tar.gz: 8ddb8f02f36a180b1a79c027e6ebbb5a53cc224f
3
+ metadata.gz: e1806b545b4bae0d8816bc273634b32ceee30292
4
+ data.tar.gz: 39488fbb5332721fa236a551bdb7677dd8d3655c
5
5
  SHA512:
6
- metadata.gz: 210ef6858d0c4ce898438a278ad82bea72eb4b8aad00bd7f093d082cc0eadaade36b40410af6d54b89b1e7f300024ae6b3835f08e821b79c2276f4299caa7cba
7
- data.tar.gz: c8dc0b9bff1679124392a44223fa297a50ac586a15e6cc935ed6e3ed50b7b95cba352c523a0bf3fc898d2e6a0a697ba11307e87c855257c9828e23b606c37c04
6
+ metadata.gz: 26186a39dac4d17d4de99dda4c01c009bbb6b8969f9035fef7a1427fd2187807a073ade78f5863d4a7b473e4e622eb9786b68e1ffcf466d903435a66fa4d3fb6
7
+ data.tar.gz: c638a2daad0aeb2527edd25e2e249c27a41ad2d06aa60d16113d87c03ed34c31997505c6c91124352c7bb26a2ce33aea2dbb8c2f4d3c9101ebf980c39289b813
data/README.md CHANGED
@@ -11,7 +11,7 @@ go-on-rails
11
11
  Add this line to your application's Gemfile:
12
12
 
13
13
  ```ruby
14
- gem 'go-on-rails', '~> 0.0.4'
14
+ gem 'go-on-rails', '~> 0.0.5'
15
15
  ```
16
16
 
17
17
  And then execute:
@@ -1,6 +1,7 @@
1
1
  <%- param_name_plural = table_name = @model_name.underscore.pluralize -%>
2
2
  <%- model_name_underscore = @model_name.underscore -%>
3
3
  <%- col_names = @struct_info[:col_names] -%>
4
+ <%- has_assoc = !@struct_info[:assoc_info][:has_many].empty? || !@struct_info[:assoc_info][:has_one].empty? -%>
4
5
  // The file is generated by go-on-rails, a Rails generator gem:
5
6
  // https://rubygems.org/gems/go-on-rails
6
7
  // Or on Github: https://github.com/goonr/go-on-rails
@@ -102,11 +103,14 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
102
103
  log.Println("No associated fields ard specified")
103
104
  return var_<%= param_name_plural %>, err
104
105
  }
106
+ if len(var_<%= param_name_plural %>) <= 0 {
107
+ return nil, errors.New("No results available")
108
+ }
105
109
  ids := make([]interface{}, len(var_<%= param_name_plural %>))
106
110
  for _, v := range var_<%= param_name_plural %> {
107
111
  ids = append(ids, interface{}(v.Id))
108
112
  }
109
- <%- unless @struct_info[:assoc_info][:has_many].empty? && @struct_info[:assoc_info][:has_one].empty? -%>
113
+ <%- if has_assoc -%>
110
114
  idsHolder := strings.Repeat(",?", len(ids)-1)
111
115
  for _, assoc := range assocs {
112
116
  switch assoc {
@@ -125,7 +129,7 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
125
129
  continue
126
130
  }
127
131
  for _, vv := range var_<%= v[:class_name].underscore.pluralize %> {
128
- for _, vvv := range var_<%= param_name_plural %> {
132
+ for i, vvv := range var_<%= param_name_plural %> {
129
133
  <%- if v[:foreign_key] -%>
130
134
  if vv.<%= v[:foreign_key].camelize %> == vvv.Id {
131
135
  vvv.<%= k %> = append(vvv.<%= k %>, vv)
@@ -135,6 +139,7 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
135
139
  vvv.<%= k %> = append(vvv.<%= k %>, vv)
136
140
  }
137
141
  <%- end -%>
142
+ var_<%= param_name_plural %>[i].<%= k %> = vvv.<%= k %>
138
143
  }
139
144
  }
140
145
  <%- end -%>
@@ -142,7 +147,7 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
142
147
  <%- unless @struct_info[:assoc_info][:has_one].empty? -%>
143
148
  <%- has_one = @struct_info[:assoc_info][:has_one] -%>
144
149
  <%- has_one.each do |k, v| -%>
145
- case "<%= k %>":
150
+ case "<%= k.underscore %>":
146
151
  <%- if v[:foreign_key] -%>
147
152
  where := fmt.Sprintf("<%= v[:foreign_key] %> IN (?%s)", idsHolder)
148
153
  <%- else -%>
@@ -154,7 +159,7 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
154
159
  continue
155
160
  }
156
161
  for _, vv := range var_<%= v[:class_name].underscore.pluralize %> {
157
- for _, vvv := range var_<%= param_name_plural %> {
162
+ for i, vvv := range var_<%= param_name_plural %> {
158
163
  <%- if v[:foreign_key] -%>
159
164
  if vv.<%= v[:foreign_key].camelize %> == vvv.Id {
160
165
  vvv.<%= k %> = vv
@@ -164,6 +169,7 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
164
169
  vvv.<%= k %> = vv
165
170
  }
166
171
  <%- end -%>
172
+ var_<%= param_name_plural %>[i].<%= k %> = vvv.<%= k %>
167
173
  }
168
174
  }
169
175
  <%- end -%>
@@ -400,40 +406,10 @@ func Destroy<%= @model_name %>(id int64) error {
400
406
  if err != nil {
401
407
  return err
402
408
  }
403
- <%- [:has_many, :has_one].each do |ass| -%>
404
- <%- ass_cols = @struct_info[:assoc_info][ass] -%>
405
- <%- unless ass_cols.empty? -%>
406
- <%- ass_cols.each_value do |opts| -%>
407
- <%- if opts.key? :dependent -%>
408
- <%- if opts[:through] -%>
409
- <%- case opts[:through] -%>
410
- <%- when :destroy, :delete_all -%>
411
- <%= opts[:class_name].underscore %>_ids, err := <%= opts[:through].to_s.singularize.camelize %>IntCol("<%= opts[:class_name].underscore %>_id", "<%= model_name_underscore %>_id = ?", var_<%= model_name_underscore %>.Id)
412
- if err != nil {
413
- return err
414
- }
415
- _, err = Destroy<%= opts[:class_name].pluralize %>(<%= opts[:class_name].underscore %>_ids...)
416
- if err != nil {
417
- return err
418
- }
419
- _, err = Destroy<%= opts[:through].to_s.pluralize.camelize %>Where("<%= model_name_underscore %>_id = ?", var_<%= model_name_underscore %>.Id)
420
- if err != nil {
421
- return err
422
- }
423
- <%- when :nullify -%>
424
- <%#- FIXME: Need a DeleteAssociation func -%>
425
- <%- end -%>
426
- <%- else -%>
427
- <%- if opts.key? :foreign_key -%>
428
- Destroy<%= opts[:class_name].pluralize %>Where("<%= opts[:foreign_key] %> = ?", id)
429
- <%- else -%>
430
- Destroy<%= opts[:class_name].pluralize %>Where("<%= model_name_underscore %>_id = ?", id)
431
- <%- end -%>
432
- <%- end -%>
433
- <%- end -%>
434
- <%- end -%>
435
- <%- end -%>
436
- <%- end -%>
409
+ <%- if has_assoc -%>
410
+ // not care if exec properly temporarily
411
+ destroy<%= @model_name %>Associations(id)
412
+ <%- end -%>
437
413
  return nil
438
414
  }
439
415
 
@@ -458,21 +434,9 @@ func Destroy<%= @model_name.pluralize %>(ids ...int64) (int64, error) {
458
434
  if err != nil {
459
435
  return 0, err
460
436
  }
461
- <%- [:has_many, :has_one].each do |ass| -%>
462
- <%- ass_cols = @struct_info[:assoc_info][ass] -%>
463
- <%- unless ass_cols.empty? -%>
464
- <%- ass_cols.each_value do |opts| -%>
465
- <%- if opts.key? :dependent -%>
466
- <%- if opts.key? :foreign_key -%>
467
- where := fmt.Sprintf("<%= opts[:foreign_key] %> IN (?%s)", idsHolder)
468
- Destroy<%= opts[:class_name].pluralize %>Where(where, idsT)
469
- <%- else -%>
470
- where := fmt.Sprintf("<%= model_name_underscore %>_id IN (?%s)", idsHolder)
471
- Destroy<%= opts[:class_name].pluralize %>Where(where, idsT)
472
- <%- end -%>
473
- <%- end -%>
474
- <%- end -%>
475
- <%- end -%>
437
+ <%- if has_assoc -%>
438
+ // not care if exec properly temporarily
439
+ destroy<%= @model_name %>Associations(ids...)
476
440
  <%- end -%>
477
441
  return cnt, nil
478
442
  }
@@ -487,6 +451,9 @@ func Destroy<%= @model_name.pluralize %>Where(where string, args ...interface{})
487
451
  } else {
488
452
  return 0, errors.New("No WHERE conditions provided")
489
453
  }
454
+ <%- if has_assoc -%>
455
+ ids, x_err := <%= @model_name %>IdsWhere(where, args...)
456
+ <%- end -%>
490
457
  stmt, err := db.Preparex(db.Rebind(sql))
491
458
  result, err := stmt.Exec(args...)
492
459
  if err != nil {
@@ -496,9 +463,86 @@ func Destroy<%= @model_name.pluralize %>Where(where string, args ...interface{})
496
463
  if err != nil {
497
464
  return 0, err
498
465
  }
466
+ <%- if has_assoc -%>
467
+ if x_err != nil {
468
+ log.Printf("Delete associated objects error: %v\n", err)
469
+ } else {
470
+ destroy<%= @model_name %>Associations(ids...)
471
+ }
472
+ <%- end -%>
499
473
  return cnt, nil
500
474
  }
501
475
 
476
+ <%- if has_assoc -%>
477
+ // the func not return err temporarily
478
+ func destroy<%= @model_name %>Associations(ids ...int64) {
479
+ idsHolder := ""
480
+ if len(ids) > 1 {
481
+ idsHolder = strings.Repeat(",?", len(ids)-1)
482
+ }
483
+ idsT := []interface{}{}
484
+ for _, id := range ids {
485
+ idsT = append(idsT, interface{}(id))
486
+ }
487
+ var err error
488
+ // make sure no declared-and-not-used exception
489
+ _, _, _ = idsHolder, idsT, err
490
+ <%- [:has_many, :has_one].each do |ass| -%>
491
+ <%- ass_cols = @struct_info[:assoc_info][ass] -%>
492
+ <%- unless ass_cols.empty? -%>
493
+ <%- ass_cols.each_value do |opts| -%>
494
+ <%- if opts.key? :dependent -%>
495
+ <%- case opts[:dependent] -%>
496
+ <%- when :destroy, :delete_all -%>
497
+ <%- if opts[:through] -%>
498
+ sql := fmt.Sprintf("id IN (SELECT id FROM <%= opts[:through].to_s %> WHERE <%= model_name_underscore %>_id IN (?%s))", idsHolder)
499
+ _, err = Destroy<%= opts[:class_name].pluralize %>Where(sql, idsT...)
500
+ if err != nil {
501
+ log.Printf("Destroy associated object %s error: %v\n", "<%= opts[:class_name].pluralize %>", err)
502
+ }
503
+ sql = fmt.Sprintf("<%= model_name_underscore %>_id IN (?%s)", idsHolder)
504
+ _, err = Destroy<%= opts[:through].to_s.pluralize.camelize %>Where(sql, idsT...)
505
+ if err != nil {
506
+ log.Printf("Destroy associated object %s error: %v\n", "<%= opts[:through].to_s.singularize.camelize %>", err)
507
+ }
508
+ <%- else -%>
509
+ <%- if opts.key? :foreign_key -%>
510
+ sql := fmt.Sprintf("<%= opts[:foreign_key] %> IN (?%s)", idsHolder)
511
+ <%- else -%>
512
+ sql := fmt.Sprintf("<%= model_name_underscore %>_id IN (?%s)", idsHolder)
513
+ <%- end -%>
514
+ _, err = Destroy<%= opts[:class_name].pluralize %>Where(sql, idsT...)
515
+ if err != nil {
516
+ log.Printf("Destroy associated object %s error: %v\n", "<%= opts[:class_name].pluralize %>", err)
517
+ }
518
+ <%- end -%>
519
+ <%- when :nullify -%>
520
+ // no sql.NULLType supported, just set the associated field to zero value of int64
521
+ <%- if opts[:through] -%>
522
+ sql := fmt.Sprintf("UPDATE <%= opts[:through].to_s %> SET <%= opts[:class_name].underscore %>_id = 0 WHERE <%= opts[:class_name].underscore %>_id IN (?%s)", idsHolder)
523
+ _, err = Update<%= opts[:through].to_s.camelize %>BySql(sql, idsT...)
524
+ if err != nil {
525
+ log.Printf("Delete associated object %s error: %v\n", "<%= opts[:class_name].pluralize %>", err)
526
+ }
527
+ <%- else -%>
528
+ <%- if opts.key? :foreign_key -%>
529
+ sql := fmt.Sprintf("UPDATE <%= opts[:class_name].underscore.pluralize %> SET <%= opts[:foreign_key] %> = 0 WHERE <%= opts[:foreign_key] %> IN (?%s)", idsHolder)
530
+ <%- else -%>
531
+ sql := fmt.Sprintf("UPDATE <%= opts[:class_name].underscore.pluralize %> SET <%= model_name_underscore %>_id = 0 WHERE <%= model_name_underscore %>_id IN (?%s)", idsHolder)
532
+ <%- end -%>
533
+ _, err = Update<%= opts[:class_name].pluralize %>BySql(sql, idsT...)
534
+ if err != nil {
535
+ log.Printf("Delete associated object %s error: %v\n", "<%= opts[:class_name].pluralize %>", err)
536
+ }
537
+ <%- end -%>
538
+ <%- end -%>
539
+ <%- end -%>
540
+ <%- end -%>
541
+ <%- end -%>
542
+ <%- end -%>
543
+ }
544
+ <%- end -%>
545
+
502
546
  func (var_<%= model_name_underscore %> *<%= @model_name %>) Save() error {
503
547
  if var_<%= model_name_underscore %>.Id == 0 {
504
548
  return errors.New("Invalid Id field: it can't be a zero value")
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.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - B1nj0y
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-17 00:00:00.000000000 Z
11
+ date: 2017-04-20 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