go-on-rails 0.0.6 → 0.0.7

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: 87622b584504d99202fecac4cf0f4c6caadd3ba0
4
- data.tar.gz: 6a0bbbe2d109dd4d4c7f694314c09ddcab6c6ff6
3
+ metadata.gz: 893993277d5b0ef18340c36e309155b3e34cf3d4
4
+ data.tar.gz: 68709edcb71c6e4f1f8ce3dffb25ae4276e2623d
5
5
  SHA512:
6
- metadata.gz: 54af6ad25951016830e0d5561d6d9baa80ff2644736c2f4d1ed286b3880f3dd2a9876e19a6bac242081aac17b67be8a2ad9817f47e0970e193ae60431a7b012c
7
- data.tar.gz: 38b7bd772720660cd9ef5372086e866c0fe5297f364c4f766c2fd711e66a218c4dc3c4195ca2b16e9257ab3af59e3d4d9e56d2a4c8082d2718ccf899576c2965
6
+ metadata.gz: a443051416f1b2352355b3be7e4875ed3c5919c82aff0f599cfddbf68b9979547fe270d5468201eb5c94190a0e38332575f7f44f42ca117917c4862a387655fd
7
+ data.tar.gz: de5c41b9abc3fb7849dbeb9271c0d965ce083376cec0e74c602af7a7898753ced6ad8cb3ae2d90880a15f98bb6041da9149d7a4e3181acee7de3662c6b92f120
data/README.md CHANGED
@@ -21,7 +21,7 @@ One or more examples will be given later on.
21
21
  Add this line to your application's Gemfile:
22
22
 
23
23
  ```ruby
24
- gem 'go-on-rails', '~> 0.0.5'
24
+ gem 'go-on-rails', '~> 0.0.7'
25
25
  ```
26
26
 
27
27
  And then execute:
@@ -84,7 +84,11 @@ When I had the idea to convert Rails app or build Golang app with Rails tools, I
84
84
 
85
85
  ## Contributing
86
86
 
87
- - Fork the project.
87
+ There're two branches at present: `master` and `dev`.
88
+
89
+ The branch `master` has the files only for the generator needs, as the `dev` branch has a whole Rails environment for development: models, seeds for testing, and under `go_app` directory there's a file named `modles_test.go` used to test generated Golang codes.
90
+
91
+ - Fork the project switch to branch `dev`.
88
92
  - Make your feature addition or bug fix.
89
93
  - Add tests for it. This is important so I don't break it in a future version unintentionally.
90
94
  - Commit, do not mess with Rakefile or version (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
@@ -97,4 +97,4 @@ class GorGenerator < Rails::Generators::Base
97
97
  end
98
98
  end
99
99
 
100
- require_relative 'go_on_rails/converter'
100
+ require_relative 'go-on-rails/converter'
@@ -118,30 +118,42 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
118
118
  <%- has_many = @struct_info[:assoc_info][:has_many] -%>
119
119
  <%- has_many.each do |k, v| -%>
120
120
  case "<%= k.underscore.pluralize %>":
121
- <%- if v[:foreign_key] -%>
122
- where := fmt.Sprintf("<%= v[:foreign_key] %> IN (?%s)", idsHolder)
123
- <%- else -%>
124
- where := fmt.Sprintf("<%= model_name_underscore %>_id IN (?%s)", idsHolder)
125
- <%- end -%>
126
- var_<%= v[:class_name].underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>Where(where, ids...)
127
- if err != nil {
128
- log.Printf("Error when query associated objects: %v\n", assoc)
129
- continue
130
- }
131
- for _, vv := range var_<%= v[:class_name].underscore.pluralize %> {
121
+ <%- if v[:through] -%>
122
+ // FIXME: optimize the query
132
123
  for i, vvv := range var_<%= param_name_plural %> {
133
- <%- if v[:foreign_key] -%>
134
- if vv.<%= v[:foreign_key].camelize %> == vvv.Id {
135
- vvv.<%= k %> = append(vvv.<%= k %>, vv)
136
- }
137
- <%- else -%>
138
- if vv.<%= @model_name.camelize %>Id == vvv.Id {
139
- vvv.<%= k %> = append(vvv.<%= k %>, vv)
140
- }
141
- <%- end -%>
142
- var_<%= param_name_plural %>[i].<%= k %> = vvv.<%= k %>
124
+ var_<%= v[:class_name].underscore.pluralize %>, err := <%= @model_name %>Get<%= v[:class_name].pluralize %>(vvv.Id)
125
+ if err != nil {
126
+ continue
127
+ }
128
+ vvv.<%= k %> = var_<%= v[:class_name].underscore.pluralize %>
129
+ var_<%= param_name_plural %>[i] = vvv
143
130
  }
144
- }
131
+ <%- else -%>
132
+ <%- if v[:foreign_key] -%>
133
+ where := fmt.Sprintf("<%= v[:foreign_key] %> IN (?%s)", idsHolder)
134
+ <%- else -%>
135
+ where := fmt.Sprintf("<%= model_name_underscore %>_id IN (?%s)", idsHolder)
136
+ <%- end -%>
137
+ var_<%= v[:class_name].underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>Where(where, ids...)
138
+ if err != nil {
139
+ log.Printf("Error when query associated objects: %v\n", assoc)
140
+ continue
141
+ }
142
+ for _, vv := range var_<%= v[:class_name].underscore.pluralize %> {
143
+ for i, vvv := range var_<%= param_name_plural %> {
144
+ <%- if v[:foreign_key] -%>
145
+ if vv.<%= v[:foreign_key].camelize %> == vvv.Id {
146
+ vvv.<%= k %> = append(vvv.<%= k %>, vv)
147
+ }
148
+ <%- else -%>
149
+ if vv.<%= @model_name.camelize %>Id == vvv.Id {
150
+ vvv.<%= k %> = append(vvv.<%= k %>, vv)
151
+ }
152
+ <%- end -%>
153
+ var_<%= param_name_plural %>[i].<%= k %> = vvv.<%= k %>
154
+ }
155
+ }
156
+ <%- end -%>
145
157
  <%- end -%>
146
158
  <%- end -%>
147
159
  <%- unless @struct_info[:assoc_info][:has_one].empty? -%>
@@ -340,25 +352,30 @@ func (var_<%= model_name_underscore %> *<%= @model_name %>) <%= v[:class_name].p
340
352
  }
341
353
 
342
354
  func (var_<%= model_name_underscore %> *<%= @model_name %>) Get<%= v[:class_name].pluralize %>() error {
355
+ var_<%= v[:class_name].underscore.pluralize %>, err := <%= @model_name %>Get<%= v[:class_name].pluralize %>(var_<%= model_name_underscore %>.Id)
356
+ if err == nil {
357
+ var_<%= model_name_underscore %>.<%= k %> = var_<%= v[:class_name].underscore.pluralize %>
358
+ }
359
+ return err
360
+ }
361
+
362
+ func <%= @model_name %>Get<%= v[:class_name].pluralize %>(id int64) ([]<%= v[:class_name] %>, error) {
343
363
  <%- if v[:through] -%>
344
364
  // FIXME: use transaction to create these associated objects
345
- <%= v[:class_name].underscore %>_ids, err := <%= v[:through].to_s.singularize.camelize %>IntCol("<%= v[:class_name].underscore %>_id", "<%= model_name_underscore %>_id = ?", var_<%= model_name_underscore %>.Id)
346
- if err != nil {
347
- return err
348
- }
349
- var_<%= v[:class_name].underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>(<%= v[:class_name].underscore %>_ids...)
365
+ <%- target_table = v[:class_name].underscore.pluralize -%>
366
+ <%- through_table = v[:through].to_s.pluralize -%>
367
+ sql := "SELECT `<%= target_table %>`.* FROM `<%= target_table %>` INNER JOIN `<%= through_table %>` ON `<%= target_table %>`.`id` = `<%= through_table %>`.`<%= v[:class_name].underscore %>_id` WHERE `<%= through_table %>`.`<%= model_name_underscore %>_id` = ?"
368
+ var_<%= v[:class_name].underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>BySql(sql, id)
350
369
  <%- else -%>
351
370
  <%- if v[:foreign_key] -%>
352
- var_<%= v[:class_name].underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>By("<%= v[:foreign_key] %>", var_<%= model_name_underscore %>.Id)
371
+ var_<%= v[:class_name].underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>By("<%= v[:foreign_key] %>", id)
353
372
  <%- else -%>
354
- var_<%= v[:class_name].underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>By("<%= model_name_underscore %>_id", var_<%= model_name_underscore %>.Id)
373
+ var_<%= v[:class_name].underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>By("<%= model_name_underscore %>_id", id)
355
374
  <%- end -%>
356
375
  <%- end -%>
357
- if err == nil {
358
- var_<%= model_name_underscore %>.<%= k %> = var_<%= v[:class_name].underscore.pluralize %>
359
- }
360
- return err
376
+ return var_<%= v[:class_name].underscore.pluralize %>, err
361
377
  }
378
+
362
379
  <%- end -%>
363
380
  <%- end -%>
364
381
 
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.6
4
+ version: 0.0.7
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-23 00:00:00.000000000 Z
11
+ date: 2017-05-02 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
@@ -21,8 +21,8 @@ files:
21
21
  - MIT-LICENSE
22
22
  - README.md
23
23
  - lib/generators/gor/USAGE
24
- - lib/generators/gor/go_on_rails/association.rb
25
- - lib/generators/gor/go_on_rails/converter.rb
24
+ - lib/generators/gor/go-on-rails/association.rb
25
+ - lib/generators/gor/go-on-rails/converter.rb
26
26
  - lib/generators/gor/gor_generator.rb
27
27
  - lib/generators/gor/templates/db.go.erb
28
28
  - lib/generators/gor/templates/favicon.ico