go-on-rails 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/generators/gor/go-on-rails/validator.rb +6 -0
- data/lib/generators/gor/templates/favicon.ico +0 -0
- data/lib/generators/gor/templates/gor_model.go.erb +61 -15
- data/lib/generators/gor/templates/home_controller.go.erb +4 -4
- data/lib/generators/gor/templates/main.go +11 -13
- data/lib/tasks/gor.rake +1 -2
- 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: 2ea55ef1523d312ab11f84c0caeb0fd5b8e9f37f
|
4
|
+
data.tar.gz: 8f35e3d8a97a4c5f969b528c6b3da4478e33da3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc3f84d037c2f9fa9b63df087539a414a6f8ee1fd9846832b9a6de103c7a0d16ac0306f885a48350a8a5569f1c71ecc80021864dbc4560c98b9bf87a44b8d3bc
|
7
|
+
data.tar.gz: 22da36278793791b9cc4518e33ac3f01a81ca76a33570e264df5b1d07e4813ff964b8f68c30b7b58a34c57d9ec0ba1163e35c0e303863fba6241b31f4810429a
|
data/README.md
CHANGED
@@ -43,6 +43,12 @@ module GoOnRails
|
|
43
43
|
else
|
44
44
|
rules << "IsNumeric"
|
45
45
|
end
|
46
|
+
when "ActiveModel::Validations::InclusionValidator"
|
47
|
+
[:in, :within].each do |i|
|
48
|
+
if validator.options && validator.options[i]
|
49
|
+
rules << "in(#{validator.options[i].join('|')})" if ['string', 'text'].include?(col.type.to_s)
|
50
|
+
end
|
51
|
+
end
|
46
52
|
when "ActiveRecord::Validations::LengthValidator"
|
47
53
|
if validator.options && ['string', 'text'].include?(col.type.to_s)
|
48
54
|
if validator.options[:is]
|
Binary file
|
@@ -38,6 +38,52 @@ func Find<%= @model_name %>(id int64) (*<%= @model_name %>, error) {
|
|
38
38
|
return &var_<%= model_name_underscore %>, nil
|
39
39
|
}
|
40
40
|
|
41
|
+
// First<%= @model_name %> find the first one <%= model_name_underscore %> by id ASC order
|
42
|
+
func First<%= @model_name %>() (*<%= @model_name %>, error) {
|
43
|
+
var_<%= model_name_underscore %> := <%= @model_name %>{}
|
44
|
+
err := db.Get(&var_<%= model_name_underscore %>, db.Rebind(`SELECT * FROM <%= table_name %> ORDER BY id ASC LIMIT 1`))
|
45
|
+
if err != nil {
|
46
|
+
log.Printf("Error: %v\n", err)
|
47
|
+
return nil, err
|
48
|
+
}
|
49
|
+
return &var_<%= model_name_underscore %>, nil
|
50
|
+
}
|
51
|
+
|
52
|
+
// First<%= @model_name.pluralize %> find the first N <%= model_name_underscore.pluralize %> by id ASC order
|
53
|
+
func First<%= @model_name.pluralize %>(n uint32) ([]<%= @model_name %>, error) {
|
54
|
+
var_<%= param_name_plural %> := []<%= @model_name %>{}
|
55
|
+
sql := fmt.Sprintf("SELECT * FROM <%= table_name %> ORDER BY id ASC LIMIT %v", n)
|
56
|
+
err := db.Select(&var_<%= param_name_plural %>, db.Rebind(sql))
|
57
|
+
if err != nil {
|
58
|
+
log.Printf("Error: %v\n", err)
|
59
|
+
return nil, err
|
60
|
+
}
|
61
|
+
return var_<%= param_name_plural %>, nil
|
62
|
+
}
|
63
|
+
|
64
|
+
// Last<%= @model_name %> find the last one <%= model_name_underscore %> by id DESC order
|
65
|
+
func Last<%= @model_name %>() (*<%= @model_name %>, error) {
|
66
|
+
var_<%= model_name_underscore %> := <%= @model_name %>{}
|
67
|
+
err := db.Get(&var_<%= model_name_underscore %>, db.Rebind(`SELECT * FROM <%= table_name %> ORDER BY id DESC LIMIT 1`))
|
68
|
+
if err != nil {
|
69
|
+
log.Printf("Error: %v\n", err)
|
70
|
+
return nil, err
|
71
|
+
}
|
72
|
+
return &var_<%= model_name_underscore %>, nil
|
73
|
+
}
|
74
|
+
|
75
|
+
// Last<%= @model_name.pluralize %> find the last N <%= model_name_underscore.pluralize %> by id DESC order
|
76
|
+
func Last<%= @model_name.pluralize %>(n uint32) ([]<%= @model_name %>, error) {
|
77
|
+
var_<%= param_name_plural %> := []<%= @model_name %>{}
|
78
|
+
sql := fmt.Sprintf("SELECT * FROM <%= table_name %> ORDER BY id DESC LIMIT %v", n)
|
79
|
+
err := db.Select(&var_<%= param_name_plural %>, db.Rebind(sql))
|
80
|
+
if err != nil {
|
81
|
+
log.Printf("Error: %v\n", err)
|
82
|
+
return nil, err
|
83
|
+
}
|
84
|
+
return var_<%= param_name_plural %>, nil
|
85
|
+
}
|
86
|
+
|
41
87
|
// Find<%= @model_name.pluralize %> find one or more <%= model_name_underscore.pluralize %> by one or more ids
|
42
88
|
func Find<%= @model_name.pluralize %>(ids ...int64) ([]<%= @model_name %>, error) {
|
43
89
|
if len(ids) == 0 {
|
@@ -429,15 +475,16 @@ func (var_<%= model_name_underscore %> *<%= @model_name %>) Destroy() error {
|
|
429
475
|
}
|
430
476
|
|
431
477
|
func Destroy<%= @model_name %>(id int64) error {
|
478
|
+
<%- if @struct_info[:has_assoc_dependent] -%>
|
479
|
+
// Destroy association objects at first
|
480
|
+
// Not care if exec properly temporarily
|
481
|
+
destroy<%= @model_name %>Associations(id)
|
482
|
+
<%- end -%>
|
432
483
|
stmt, err := db.Preparex(db.Rebind(`DELETE FROM <%= table_name %> WHERE id = ?`))
|
433
484
|
_, err = stmt.Exec(id)
|
434
485
|
if err != nil {
|
435
486
|
return err
|
436
487
|
}
|
437
|
-
<%- if @struct_info[:has_assoc_dependent] -%>
|
438
|
-
// not care if exec properly temporarily
|
439
|
-
destroy<%= @model_name %>Associations(id)
|
440
|
-
<%- end -%>
|
441
488
|
return nil
|
442
489
|
}
|
443
490
|
|
@@ -447,6 +494,11 @@ func Destroy<%= @model_name.pluralize %>(ids ...int64) (int64, error) {
|
|
447
494
|
log.Println(msg)
|
448
495
|
return 0, errors.New(msg)
|
449
496
|
}
|
497
|
+
<%- if @struct_info[:has_assoc_dependent] -%>
|
498
|
+
// Destroy association objects at first
|
499
|
+
// Not care if exec properly temporarily
|
500
|
+
destroy<%= @model_name %>Associations(ids...)
|
501
|
+
<%- end -%>
|
450
502
|
idsHolder := strings.Repeat(",?", len(ids)-1)
|
451
503
|
sql := fmt.Sprintf(`DELETE FROM <%= table_name %> WHERE id IN (?%s)`, idsHolder)
|
452
504
|
idsT := []interface{}{}
|
@@ -462,10 +514,6 @@ func Destroy<%= @model_name.pluralize %>(ids ...int64) (int64, error) {
|
|
462
514
|
if err != nil {
|
463
515
|
return 0, err
|
464
516
|
}
|
465
|
-
<%- if @struct_info[:has_assoc_dependent] -%>
|
466
|
-
// not care if exec properly temporarily
|
467
|
-
destroy<%= @model_name %>Associations(ids...)
|
468
|
-
<%- end -%>
|
469
517
|
return cnt, nil
|
470
518
|
}
|
471
519
|
|
@@ -481,6 +529,11 @@ func Destroy<%= @model_name.pluralize %>Where(where string, args ...interface{})
|
|
481
529
|
}
|
482
530
|
<%- if @struct_info[:has_assoc_dependent] -%>
|
483
531
|
ids, x_err := <%= @model_name %>IdsWhere(where, args...)
|
532
|
+
if x_err != nil {
|
533
|
+
log.Printf("Delete associated objects error: %v\n", x_err)
|
534
|
+
} else {
|
535
|
+
destroy<%= @model_name %>Associations(ids...)
|
536
|
+
}
|
484
537
|
<%- end -%>
|
485
538
|
stmt, err := db.Preparex(db.Rebind(sql))
|
486
539
|
result, err := stmt.Exec(args...)
|
@@ -491,13 +544,6 @@ func Destroy<%= @model_name.pluralize %>Where(where string, args ...interface{})
|
|
491
544
|
if err != nil {
|
492
545
|
return 0, err
|
493
546
|
}
|
494
|
-
<%- if @struct_info[:has_assoc_dependent] -%>
|
495
|
-
if x_err != nil {
|
496
|
-
log.Printf("Delete associated objects error: %v\n", err)
|
497
|
-
} else {
|
498
|
-
destroy<%= @model_name %>Associations(ids...)
|
499
|
-
}
|
500
|
-
<%- end -%>
|
501
547
|
return cnt, nil
|
502
548
|
}
|
503
549
|
|
@@ -1,13 +1,14 @@
|
|
1
1
|
package controller
|
2
2
|
|
3
3
|
import (
|
4
|
-
"html/template"
|
5
4
|
"net/http"
|
5
|
+
|
6
|
+
"gopkg.in/gin-gonic/gin.v1"
|
6
7
|
// you can import models
|
7
8
|
//m "../models"
|
8
9
|
)
|
9
10
|
|
10
|
-
func HomeHandler(
|
11
|
+
func HomeHandler(c *gin.Context) {
|
11
12
|
// you can use model functions to do CRUD
|
12
13
|
//
|
13
14
|
// user, _ := m.FindUser(1)
|
@@ -26,6 +27,5 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) {
|
|
26
27
|
golangVer := "<%= `go version`.chomp rescue "Failed to get" %>"
|
27
28
|
|
28
29
|
envs := Envs{GoOnRailsVer: gorVer, GolangVer: golangVer}
|
29
|
-
|
30
|
-
t.Execute(w, envs)
|
30
|
+
c.HTML(http.StatusOK, "index.tmpl", envs)
|
31
31
|
}
|
@@ -2,28 +2,26 @@ package main
|
|
2
2
|
|
3
3
|
import (
|
4
4
|
"flag"
|
5
|
-
"log"
|
6
|
-
"net/http"
|
7
|
-
"os"
|
8
5
|
|
9
6
|
c "./controllers"
|
10
|
-
"
|
11
|
-
"github.com/gorilla/mux"
|
7
|
+
"gopkg.in/gin-gonic/gin.v1"
|
12
8
|
)
|
13
9
|
|
14
10
|
func main() {
|
15
11
|
// The app will run on port 3000 by default, you can custom it with the flag -port
|
16
12
|
servePort := flag.String("port", "3000", "Http Server Port")
|
17
13
|
flag.Parse()
|
18
|
-
log.Printf("The application starting on the port: [%v]\n", *servePort)
|
19
14
|
|
20
|
-
// Here we are instantiating the
|
21
|
-
r :=
|
22
|
-
//
|
23
|
-
|
15
|
+
// Here we are instantiating the router
|
16
|
+
r := gin.Default()
|
17
|
+
// Switch to "release" mode in production
|
18
|
+
// gin.SetMode(gin.ReleaseMode)
|
19
|
+
r.LoadHTMLGlob("views/*")
|
24
20
|
// Create a static assets router
|
25
|
-
r.
|
26
|
-
|
21
|
+
// r.Static("/assets", "./public/assets")
|
22
|
+
r.StaticFile("/favicon.ico", "./public/favicon.ico")
|
23
|
+
// Then we bind some route to some handler(controller action)
|
24
|
+
r.GET("/", c.HomeHandler)
|
27
25
|
// Let's start the server
|
28
|
-
|
26
|
+
r.Run(":" + *servePort)
|
29
27
|
}
|
data/lib/tasks/gor.rake
CHANGED
@@ -4,8 +4,7 @@ namespace :gor do
|
|
4
4
|
puts 'Beginning to install Go deps...'
|
5
5
|
system "go get \
|
6
6
|
github.com/jmoiron/sqlx \
|
7
|
-
|
8
|
-
github.com/gorilla/mux \
|
7
|
+
gopkg.in/gin-gonic/gin.v1 \
|
9
8
|
github.com/mattn/go-sqlite3 \
|
10
9
|
github.com/go-sql-driver/mysql \
|
11
10
|
github.com/lib/pq \
|
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
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- B1nj0y
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-10 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
|