ng_on_rails 0.0.3 → 0.0.3.1
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 +63 -5
- data/app/assets/javascripts/directives/shared.js.coffee +35 -0
- data/lib/generators/ng_on_rails/controller_generator.rb +19 -0
- data/lib/generators/ng_on_rails/templates/controller_template.js.erb +22 -4
- data/lib/generators/ng_on_rails/templates/styles_template.css +107 -0
- data/lib/generators/ng_on_rails/templates/views/slim/_form.html.erb +33 -0
- data/lib/generators/ng_on_rails/templates/views/slim/_model.html.erb +37 -0
- data/lib/generators/ng_on_rails/templates/views/slim/_show.html.erb +9 -0
- data/lib/generators/ng_on_rails/templates/views/slim/index.html.erb +15 -0
- data/lib/generators/ng_on_rails/templates/views/slim/show.html.erb +35 -0
- data/lib/generators/ng_on_rails/views_generator.rb +211 -0
- data/lib/ng_on_rails/version.rb +1 -1
- metadata +10 -3
- data/app/assets/javascripts/directives/shared.js.erb +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9cf65bbace77206f0ab8dd0945f239d1a2bb2617
|
4
|
+
data.tar.gz: 2954f08cd74e244f60c302ae6895415391dbb7f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00798198b11443ce8b6d67c172176166532b49f29cbac5c660ff0a4c221334c5034ae8612b1ca93ee0fc888a4ea665e593b85555412e2fb9c0b841bfa93f7762
|
7
|
+
data.tar.gz: 11618b116368700a91aaa7cb53c7f1805188977526d0b4e09f2d8211b99e2466a0eb942f8625878cf937b05be72b88961546043e09ef3f7f07a65d49ee4971df
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
##### *This project is
|
1
|
+
##### *This project is in active developement. MIT-LICENSE.*
|
2
2
|
|
3
3
|
-----------------------------------------------------------
|
4
4
|
|
@@ -8,10 +8,7 @@
|
|
8
8
|
|
9
9
|
The main motivations behind this gem is to standardize and simplify how AngularJS is integrated in a rails application.
|
10
10
|
|
11
|
-
|
12
|
-
* Write more specs!!!
|
13
|
-
* (Create generators for views?)
|
14
|
-
* (ViewHelper functions via shared service?)
|
11
|
+
This project is in active development. Check back often for updates and be very careful when using with any production app.
|
15
12
|
|
16
13
|
### Install it!
|
17
14
|
```
|
@@ -66,6 +63,67 @@ You are now up and running! To generate controllers and resource-services use Ng
|
|
66
63
|
# Assuming the Rails app has a "Page" model:
|
67
64
|
$ bundle exec rails g ng_on_rails:controller Page
|
68
65
|
$ bundle exec rails g ng_on_rails:resource Page
|
66
|
+
$ bundle exec rails g ng_on_rails:views Page
|
67
|
+
```
|
68
|
+
`ng_on_rails:controller` has one option, `[--belongs-to=one two three] # list of models it belongs_to` setting this option will ensure that belongs to relationships are set in the "rest.new()" and "rest.edit()" methods
|
69
|
+
|
70
|
+
|
71
|
+
`ng_on_rails:views` has many options:
|
72
|
+
```
|
73
|
+
$ bundle exec rails g ng_on_rails:views --help
|
74
|
+
Usage:
|
75
|
+
rails generate ng_on_rails:views MODEL_NAME [options]
|
76
|
+
|
77
|
+
Options:
|
78
|
+
[--properties=one two three] # list of properties
|
79
|
+
[--relationships=one two three] # list of relationships. determines has_many/one from singular/plural name
|
80
|
+
[--format=FORMAT] # *** FOR NOW ONLY OFFERS SLIM*** templating engine. defaults to slim. slim, haml, erb
|
81
|
+
# Default: slim
|
82
|
+
[--render-views], [--no-render-views] # Insert render_view directives into rails-views
|
83
|
+
# Default: true
|
84
|
+
[--styles], [--no-styles] # add ng_on_rails_styles.css
|
85
|
+
# Default: true
|
86
|
+
[--belongs-to=one two three] # list of models it belongs_to
|
87
|
+
```
|
88
|
+
* `--properties` is a list of properties you want in the views. A property looks like `property_name:property_type{opt1+opt2+...}`.
|
89
|
+
* property\_name: (required) name of the property
|
90
|
+
* property\_type: (optional) number/textarea -- default empty
|
91
|
+
* opt-list: (optional) seperate options by "+". the allowed values are:
|
92
|
+
* required: make the property required in the form
|
93
|
+
* skip_form: do not include in the form
|
94
|
+
* skip_index: do not include in the index table row
|
95
|
+
* link: link this property in index table to the show view
|
96
|
+
|
97
|
+
A typical example might look like this
|
98
|
+
```
|
99
|
+
bundle exec rails g ng_on_rails:views Doc --properties id:number{skip_form+link} name{required} description:textarea{skip_index}
|
100
|
+
```
|
101
|
+
* `--format, --styles` should be self explanatory.
|
102
|
+
* `--render-views=true` will append (creating file if necessary) code to load the angular views to your index and show views in your views directory. For example, your index files becomes:
|
103
|
+
```slim
|
104
|
+
# your_app/app/views/docs/index.html.slim
|
105
|
+
|
106
|
+
... your content ...
|
107
|
+
|
108
|
+
/
|
109
|
+
/ Inserted by NgOnRails view generator.
|
110
|
+
/
|
111
|
+
div ng-init="docs=ctrl.rails.docs" render_view="true" url="docs/index"
|
112
|
+
/
|
113
|
+
/
|
114
|
+
/
|
115
|
+
```
|
116
|
+
* `--belongs_to` same as for ng\_on\_rails:controller generator [include the index view of the related model(s) on the show page]
|
117
|
+
|
118
|
+
##### Test App
|
119
|
+
The [test_app](https://github.com/brookisme/ng_on_rails/tree/master/spec/test_app) serves as an example of how to set up a project. With one minor alteration the test app was generated with the following commands:
|
120
|
+
```
|
121
|
+
$ bundle exec rails g ng_on_rails:resource Doc
|
122
|
+
$ bundle exec rails g ng_on_rails:controller Doc
|
123
|
+
$ bundle exec rails g ng_on_rails:views Doc --properties id:number{skip_form+link} name{required} description:textarea{skip_index} --relationships pages
|
124
|
+
$ bundle exec rails g ng_on_rails:resource Page
|
125
|
+
$ bundle exec rails g ng_on_rails:controller Page --belongs_to Doc
|
126
|
+
$ bundle exec rails g ng_on_rails:views Page --properties id:number{skip_form+link} order_index:number subject{required} body:textarea{skip_index} --belongs_to Doc
|
69
127
|
```
|
70
128
|
|
71
129
|
##### Service: Rails
|
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# NgOnRails: Render Directives
|
4
|
+
#
|
5
|
+
|
6
|
+
NgOnRailsApp.directive "renderView", ->
|
7
|
+
restrict: "AE",
|
8
|
+
transclude: true,
|
9
|
+
template: (el,attrs)->
|
10
|
+
format = attrs.format || "html"
|
11
|
+
'<div ng_include="\'/angular_app/'+attrs.url+'.'+format+'\'"></div>'
|
12
|
+
|
13
|
+
NgOnRailsApp.directive "render", ->
|
14
|
+
restrict: "AE",
|
15
|
+
transclude: true,
|
16
|
+
template: (el,attrs)->
|
17
|
+
format = attrs.format || "html"
|
18
|
+
url_parts = attrs.url.split("/")
|
19
|
+
if (url_parts[url_parts.length-1].trim()=="")
|
20
|
+
url_parts.pop()
|
21
|
+
last = url_parts.pop()
|
22
|
+
url_parts.push("_"+last)
|
23
|
+
path = url_parts.join("/")
|
24
|
+
'<div ng_include="\'/angular_app/'+path+'.'+format+'\'"></div>'
|
25
|
+
|
26
|
+
|
27
|
+
#
|
28
|
+
# NgOnRails: View Helper Directives
|
29
|
+
#
|
30
|
+
|
31
|
+
NgOnRailsApp.directive "confirm", ->
|
32
|
+
link: (scope, el, attrs)->
|
33
|
+
msg = attrs.confirm || "Are you sure?"
|
34
|
+
el.bind 'click', ->
|
35
|
+
scope.$eval(attrs.action) if window.confirm(msg)
|
@@ -2,6 +2,7 @@ require 'rails/generators'
|
|
2
2
|
module NgOnRails
|
3
3
|
class ControllerGenerator < Rails::Generators::NamedBase
|
4
4
|
desc "Creates NgOnRails-style AngularJS Controller"
|
5
|
+
class_option :belongs_to, type: :array, required: false, default: [], desc: "list of models it belongs_to"
|
5
6
|
|
6
7
|
def self.source_root
|
7
8
|
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
|
@@ -10,5 +11,23 @@ module NgOnRails
|
|
10
11
|
def generate_layout
|
11
12
|
template "#{ControllerGenerator.source_root}/controller_template.js.erb", "app/assets/javascripts/angular_app/controllers/#{plural_name}_controller.js.coffee"
|
12
13
|
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def belongs_to_array
|
18
|
+
if options[:belongs_to].blank?
|
19
|
+
[]
|
20
|
+
else
|
21
|
+
options[:belongs_to].map do |a|
|
22
|
+
a.underscore.gsub(" ","")+"_id"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def belongs_to_parameters
|
28
|
+
unless options[:belongs_to].blank?
|
29
|
+
belongs_to_array.join(",")
|
30
|
+
end
|
31
|
+
end
|
13
32
|
end
|
14
33
|
end
|
@@ -29,10 +29,16 @@ NgOnRailsApp.controller '<%="#{class_name.pluralize}"%>Controller', ($scope,<%="
|
|
29
29
|
<%="#{class_name}"%>.get({id: <%="#{file_name}"%>_id}).$promise.then (<%="#{file_name}"%>) ->
|
30
30
|
ctrl.data.<%="#{file_name}"%> = <%="#{file_name}"%>
|
31
31
|
|
32
|
-
new: ()->
|
32
|
+
new: (<%=belongs_to_parameters%>)->
|
33
33
|
ctrl.clear()
|
34
34
|
ctrl.data.active<%="#{class_name}"%> = {}
|
35
35
|
ctrl.data.creating_new_<%="#{file_name}"%> = true
|
36
|
+
<%
|
37
|
+
belongs_to_array.each do |relationship|
|
38
|
+
%>ctrl.data.active<%="#{class_name}"%>.<%= relationship%> = <%= relationship %>
|
39
|
+
<%
|
40
|
+
end
|
41
|
+
%>
|
36
42
|
|
37
43
|
create: ->
|
38
44
|
if !(ctrl.locked || ctrl.<%="#{file_name}"%>_form.$error.required)
|
@@ -52,10 +58,17 @@ NgOnRailsApp.controller '<%="#{class_name.pluralize}"%>Controller', ($scope,<%="
|
|
52
58
|
ctrl.locked = false
|
53
59
|
)
|
54
60
|
|
55
|
-
edit: (<%="#{file_name}"%>) ->
|
61
|
+
edit: (<%="#{file_name}"%>,<%=belongs_to_parameters%>) ->
|
56
62
|
ctrl.clear()
|
57
63
|
ctrl.data.active<%="#{class_name}"%> = <%="#{file_name}"%>
|
58
64
|
ctrl.data.editing_<%="#{file_name}"%> = true
|
65
|
+
<%="#{file_name}"%>.is_displayed = false
|
66
|
+
<%
|
67
|
+
belongs_to_array.each do |relationship|
|
68
|
+
%>ctrl.data.active<%="#{class_name}"%>.<%= relationship%> = <%= relationship %>
|
69
|
+
<%
|
70
|
+
end
|
71
|
+
%>
|
59
72
|
|
60
73
|
update: (<%="#{file_name}"%>)->
|
61
74
|
if !(ctrl.locked || ctrl.<%="#{file_name}"%>_form.$error.required)
|
@@ -79,7 +92,10 @@ NgOnRailsApp.controller '<%="#{class_name.pluralize}"%>Controller', ($scope,<%="
|
|
79
92
|
<%="#{file_name}"%>,
|
80
93
|
(<%="#{file_name}"%>)->
|
81
94
|
<%="#{plural_name}"%> ||= ctrl.data.<%="#{plural_name}"%>
|
82
|
-
|
95
|
+
if !!<%="#{plural_name}"%>
|
96
|
+
<%="#{plural_name}"%>.splice(index,1)
|
97
|
+
else
|
98
|
+
window.location.href = '/<%="#{plural_name}"%>'
|
83
99
|
,
|
84
100
|
(error)->
|
85
101
|
console.log("delete_error:",error)
|
@@ -90,7 +106,7 @@ NgOnRailsApp.controller '<%="#{class_name.pluralize}"%>Controller', ($scope,<%="
|
|
90
106
|
#
|
91
107
|
# SCOPE METHODS
|
92
108
|
#
|
93
|
-
ctrl.clear = ->
|
109
|
+
ctrl.clear = (doc)->
|
94
110
|
ctrl.data.active<%="#{class_name}"%> = null
|
95
111
|
ctrl.data.creating_new_<%="#{file_name}"%> = false
|
96
112
|
ctrl.data.editing_<%="#{file_name}"%> = false
|
@@ -99,6 +115,8 @@ NgOnRailsApp.controller '<%="#{class_name.pluralize}"%>Controller', ($scope,<%="
|
|
99
115
|
(ctrl.data.editing_<%="#{file_name}"%> && !!<%="#{file_name}"%> && <%="#{file_name}"%>.id == ctrl.data.active<%="#{class_name}"%>.id) ||
|
100
116
|
(ctrl.data.creating_new_<%="#{file_name}"%> && !<%="#{file_name}"%>)
|
101
117
|
|
118
|
+
ctrl.toggleDisplay = (<%="#{file_name}"%>)->
|
119
|
+
<%="#{file_name}"%>.is_displayed = !<%="#{file_name}"%>.is_displayed
|
102
120
|
|
103
121
|
#
|
104
122
|
# PRIVATE METHODS
|
@@ -0,0 +1,107 @@
|
|
1
|
+
.wrapper {
|
2
|
+
margin: 25px;
|
3
|
+
}
|
4
|
+
i.fa {
|
5
|
+
color: #ccc;
|
6
|
+
}
|
7
|
+
i.fa.light,
|
8
|
+
.light {
|
9
|
+
color: #aaa;
|
10
|
+
}
|
11
|
+
i.fa.white,
|
12
|
+
.white {
|
13
|
+
color: #fff;
|
14
|
+
}
|
15
|
+
i.fa.success,
|
16
|
+
.success {
|
17
|
+
color: #5cb85c;
|
18
|
+
}
|
19
|
+
i.fa.danger,
|
20
|
+
.error,
|
21
|
+
.danger {
|
22
|
+
color: #d9534f;
|
23
|
+
}
|
24
|
+
.error {
|
25
|
+
font-size: 11px;
|
26
|
+
font-weight: bold;
|
27
|
+
text-transform: uppercase;
|
28
|
+
}
|
29
|
+
i span.ilabel {
|
30
|
+
font-size: 14px;
|
31
|
+
margin-left: 5px;
|
32
|
+
position: relative;
|
33
|
+
top: -2px;
|
34
|
+
font-style: normal;
|
35
|
+
}
|
36
|
+
.property-label {
|
37
|
+
color: #069;
|
38
|
+
font-size: 12px;
|
39
|
+
font-weight: bold;
|
40
|
+
text-transform: uppercase;
|
41
|
+
}
|
42
|
+
|
43
|
+
/** DIV Tables **/
|
44
|
+
|
45
|
+
.div-table {
|
46
|
+
border: 1px solid #ccc;
|
47
|
+
}
|
48
|
+
.div-table .tr-collection:first-child {
|
49
|
+
border-top: 1px dotted #ccc;
|
50
|
+
}
|
51
|
+
.div-table .th,
|
52
|
+
.div-table .tr {
|
53
|
+
border-bottom: 1px dotted #ccc;
|
54
|
+
padding: 5px;
|
55
|
+
}
|
56
|
+
.div-table .th {
|
57
|
+
font-size: 11px;
|
58
|
+
font-weight: bold;
|
59
|
+
color: #069;
|
60
|
+
background-color: #eee;
|
61
|
+
}
|
62
|
+
.div-table .td {
|
63
|
+
display: inline-block;
|
64
|
+
margin-right: 5px;
|
65
|
+
padding-right: 5px;
|
66
|
+
width: 250px;
|
67
|
+
}
|
68
|
+
.div-table .td-collection {
|
69
|
+
display: inline-block;
|
70
|
+
}
|
71
|
+
.div-table .tr:hover {
|
72
|
+
background: #fff;
|
73
|
+
border-bottom: 1px solid #ccc;
|
74
|
+
}
|
75
|
+
.div-table {
|
76
|
+
margin: 25px 0;
|
77
|
+
}
|
78
|
+
.div-table .td.id,
|
79
|
+
.div-table .td.number {
|
80
|
+
width: 75px;
|
81
|
+
text-align: center;
|
82
|
+
}
|
83
|
+
.div-table .td.btn-group {
|
84
|
+
width: 300px;
|
85
|
+
}
|
86
|
+
.tr.quick-view {
|
87
|
+
padding: 20px 5px;
|
88
|
+
}
|
89
|
+
|
90
|
+
|
91
|
+
/* FORMS */
|
92
|
+
.form-inline input {
|
93
|
+
margin-left: 2px;
|
94
|
+
}
|
95
|
+
input.form-control.medium {
|
96
|
+
width: 200px;
|
97
|
+
}
|
98
|
+
input[type="number"],
|
99
|
+
input[type="number"].form-control {
|
100
|
+
width: 70px;
|
101
|
+
}
|
102
|
+
form span.error {
|
103
|
+
margin-left: 15px;
|
104
|
+
}
|
105
|
+
.list_label {
|
106
|
+
display: inline-block;
|
107
|
+
}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
form.form name="ctrl.<%= resource_name %>_form" role="form" ng-show="ctrl.is_editing(<%= resource_name %>)"
|
2
|
+
.form-group
|
3
|
+
.btn-group
|
4
|
+
button.btn.btn-default ng-if="ctrl.data.creating_new_<%= resource_name %>" type="submit" ng-click="ctrl.rest.create()"
|
5
|
+
i.fa.fa-check.success
|
6
|
+
span.ilabel create
|
7
|
+
button.btn.btn-default ng-if="ctrl.data.editing_<%= resource_name %>" type="submit" ng-click="ctrl.rest.update()"
|
8
|
+
i.fa.fa-check.success
|
9
|
+
span.ilabel update
|
10
|
+
button.btn-default.btn ng-click="ctrl.clear(<%= resource_name %>)"
|
11
|
+
i.fa.fa-remove.danger
|
12
|
+
span.ilabel cancel
|
13
|
+
|
14
|
+
<% options[:properties].each do |property| %>
|
15
|
+
<% property_hash = args(property) %>
|
16
|
+
<% unless property_hash[:skip_form] %>
|
17
|
+
.form-group.<%= property_hash[:name] %><%= type_to_class(property_hash[:type]) %>
|
18
|
+
label.sr-only <%= property_hash[:name] %>:
|
19
|
+
<%
|
20
|
+
type_string = "type=\"#{property_hash[:type]}\"" unless property_hash[:type].blank?
|
21
|
+
if property_hash[:type]=="textarea"%>textarea.form-control<%= type_to_class(property_hash[:type])
|
22
|
+
%> name='<%= property_hash[:name] %>' <%= required_string(property_hash) %> ng-model="ctrl.data.active<%= class_name %>.<%= property_hash[:name] %>" placeholder="<%= property_hash[:name] %>"<%
|
23
|
+
else
|
24
|
+
%>input.form-control<%= type_to_class(property_hash[:type]) %> <%=type_string%> name='<%= property_hash[:name] %>' <%= required_string(property_hash) %> ng-model="ctrl.data.active<%= class_name %>.<%= property_hash[:name] %>" placeholder="<%= property_hash[:name] %>"<%
|
25
|
+
end
|
26
|
+
%>
|
27
|
+
<%
|
28
|
+
if property_hash[:required]
|
29
|
+
%>span.error ng-show="ctrl.<%= resource_name %>_form.name.$error.required" Required!<%
|
30
|
+
end
|
31
|
+
%>
|
32
|
+
<% end %>
|
33
|
+
<% end %>
|
@@ -0,0 +1,37 @@
|
|
1
|
+
.tr
|
2
|
+
<%
|
3
|
+
options[:properties].each do |property|
|
4
|
+
property_hash = args(property)
|
5
|
+
unless property_hash[:skip_index]
|
6
|
+
@name_rep ||= property_hash[:name]
|
7
|
+
if property_hash[:link]
|
8
|
+
%>.td.<%= property_hash[:name] %><%= type_to_class(property_hash[:type]) %>
|
9
|
+
a href="/<%= plural_name %>/{{<%= resource_name %>.id}}"
|
10
|
+
span ng-bind="<%= resource_name %>.<%= property_hash[:name] %>"
|
11
|
+
<%
|
12
|
+
else
|
13
|
+
%>.td.<%= property_hash[:name] %><%= type_to_class(property_hash[:type]) %>
|
14
|
+
span ng-bind="<%= resource_name %>.<%= property_hash[:name] %>"
|
15
|
+
<% end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
%>
|
19
|
+
.td.btn-group
|
20
|
+
button.btn.btn-default ng-click="ctrl.toggleDisplay(<%= resource_name %>)"
|
21
|
+
i.fa.fa-plus-square-o.fa-lg ng-hide="<%= resource_name %>.displaying_<%= resource_name %>"
|
22
|
+
span.ilabel show
|
23
|
+
i.fa.fa-minus-square-o.fa-lg ng-show="<%= resource_name %>.displaying_<%= resource_name %>"
|
24
|
+
span.ilabel hide
|
25
|
+
button.btn.btn-default ng-click="ctrl.rest.edit(<%= resource_name %>,<%= resource_name %>.id)"
|
26
|
+
i.fa.fa-edit.fa-lg
|
27
|
+
span.ilabel edit
|
28
|
+
button.btn.btn-default confirm="Do you really want to delete <%= resource_name %>: '{{<%= resource_name %>.<%=@name_rep %>}}'?" action="ctrl.rest.delete($index,<%= resource_name %>)"
|
29
|
+
i.fa.fa-remove.fa-lg.danger
|
30
|
+
span.ilabel delete
|
31
|
+
.tr.quick-view ng-show='<%= resource_name %>.is_displayed'
|
32
|
+
.td
|
33
|
+
div render="true" url="<%= plural_name %>/show"
|
34
|
+
|
35
|
+
.tr.quick-view ng-show='ctrl.is_editing(<%= resource_name %>)'
|
36
|
+
.td
|
37
|
+
div render="true" url="<%= plural_name %>/form"
|
@@ -0,0 +1,9 @@
|
|
1
|
+
<%
|
2
|
+
options[:properties].each do |property|
|
3
|
+
property_hash = args(property)
|
4
|
+
%>
|
5
|
+
.property-label.<%= property_hash[:name] %><%= type_to_class(property_hash[:type]) %> <%= property_hash[:name] %>:
|
6
|
+
div ng-bind="<%= resource_name %>.<%= property_hash[:name] %>"
|
7
|
+
<%
|
8
|
+
end
|
9
|
+
%>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
div ng_controller="<%= class_name.pluralize %>Controller as ctrl" ng-init="ctrl.set<%= class_name.pluralize %>(<%= plural_name %>)"
|
2
|
+
h3 <%=class_name.pluralize%>
|
3
|
+
button.btn.btn-default ng-hide="ctrl.data.creating_new_<%= resource_name %>" ng-click="ctrl.rest.new(<%= belongs_to_values %>)" new
|
4
|
+
div render="true" url="<%= plural_name %>/form"
|
5
|
+
.div-table
|
6
|
+
.th<%
|
7
|
+
options[:properties].each do |property|
|
8
|
+
property_hash = args(property)
|
9
|
+
unless property_hash[:skip_index]
|
10
|
+
%>
|
11
|
+
.td.<%= property_hash[:name] %><%= type_to_class(property_hash[:type]) %> <%= property_hash[:name] %><%
|
12
|
+
end
|
13
|
+
end
|
14
|
+
%>
|
15
|
+
.tr-collection ng-repeat="<%= resource_name %> in ctrl.data.<%= plural_name %>" render="true" url="<%= plural_name %>/<%= resource_name %>"
|
@@ -0,0 +1,35 @@
|
|
1
|
+
div ng_controller="<%= class_name.pluralize %>Controller as ctrl" ng-init="ctrl.set<%= class_name.pluralize %>(<%= plural_name %>,<%= resource_name %>)"
|
2
|
+
h3
|
3
|
+
| <%= class_name %>:
|
4
|
+
<%
|
5
|
+
options[:properties].each do |property|
|
6
|
+
property_hash = args(property)
|
7
|
+
@name_rep ||= property_hash[:name]
|
8
|
+
%>
|
9
|
+
.property-label.<%= property_hash[:name] %><%= type_to_class(property_hash[:type]) %> <%= property_hash[:name] %>:
|
10
|
+
div ng-bind="<%= resource_name %>.<%= property_hash[:name] %>"
|
11
|
+
<%
|
12
|
+
end
|
13
|
+
%>
|
14
|
+
button.btn.btn-default ng-click="ctrl.rest.edit(<%= resource_name %><%= belongs_to_comma %><%= belongs_to_values %>)"
|
15
|
+
i.fa.fa-edit.fa-lg
|
16
|
+
span.ilabel edit
|
17
|
+
button.btn.btn-default confirm="Do you really want to delete <%= resource_name %>: '{{<%= resource_name %>.<%=@name_rep %>}}'?" action="ctrl.rest.delete($index,<%= resource_name %>)"
|
18
|
+
i.fa.fa-remove.fa-lg.danger
|
19
|
+
span.ilabel delete
|
20
|
+
|
21
|
+
.formbox ng-show='ctrl.is_editing(<%= resource_name %>)'
|
22
|
+
div render="true" url="<%= plural_name %>/form"
|
23
|
+
|
24
|
+
<%
|
25
|
+
options[:relationships].each do |relationship|
|
26
|
+
if is_plural?(relationship)
|
27
|
+
relationship_page = "index"
|
28
|
+
else
|
29
|
+
relationship_page = "show"
|
30
|
+
end
|
31
|
+
%>
|
32
|
+
div render_view="true" url="<%= relationship.pluralize %>/<%= relationship_page %>" ng-init="<%= relationship %>=<%= resource_name %>.<%= relationship %>"
|
33
|
+
<%
|
34
|
+
end
|
35
|
+
%>
|
@@ -0,0 +1,211 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
module NgOnRails
|
3
|
+
class ViewsGenerator < Rails::Generators::Base
|
4
|
+
desc "Creates NgOnRails-style AngularJS Views"
|
5
|
+
|
6
|
+
# arguments
|
7
|
+
argument :model_name, type: :string, required: true, desc: "required"
|
8
|
+
class_option :properties, type: :array, required: false, default: [], desc: "list of properties"
|
9
|
+
class_option :relationships, type: :array, required: false, default: [], desc: "list of relationships. determines has_many/one from singular/plural name"
|
10
|
+
class_option :format, type: :string, required: false, default: "slim", desc: "*** FOR NOW ONLY OFFERS SLIM*** templating engine. defaults to slim. slim, haml, erb"
|
11
|
+
class_option :render_views, type: :boolean, required: false, default: true, desc: "Insert render_view directives into rails-views"
|
12
|
+
class_option :styles, type: :boolean, required: false, default: true, desc: "add ng_on_rails_styles.css"
|
13
|
+
class_option :belongs_to, type: :array, required: false, default: [], desc: "list of models it belongs_to"
|
14
|
+
|
15
|
+
def self.source_root
|
16
|
+
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
|
17
|
+
end
|
18
|
+
|
19
|
+
def generate_layout
|
20
|
+
template "#{ViewsGenerator.source_root}/views/#{options[:format]}/index.html.erb",
|
21
|
+
"app/views/angular_app/#{plural_name}/index.html.#{options[:format]}"
|
22
|
+
template "#{ViewsGenerator.source_root}/views/#{options[:format]}/show.html.erb",
|
23
|
+
"app/views/angular_app/#{plural_name}/show.html.#{options[:format]}"
|
24
|
+
template "#{ViewsGenerator.source_root}/views/#{options[:format]}/_show.html.erb",
|
25
|
+
"app/views/angular_app/#{plural_name}/_show.html.#{options[:format]}"
|
26
|
+
template "#{ViewsGenerator.source_root}/views/#{options[:format]}/_form.html.erb",
|
27
|
+
"app/views/angular_app/#{plural_name}/_form.html.#{options[:format]}"
|
28
|
+
template "#{ViewsGenerator.source_root}/views/#{options[:format]}/_model.html.erb",
|
29
|
+
"app/views/angular_app/#{plural_name}/_#{resource_name}.html.#{options[:format]}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_render_views_files
|
33
|
+
if insert_the_render_views?
|
34
|
+
unless File.exist?(index_path)
|
35
|
+
puts "File[ #{index_path} ] does not exist. creating file"
|
36
|
+
create_file index_path, '/ File created with NgOnRails view generator'
|
37
|
+
end
|
38
|
+
|
39
|
+
unless File.exist?(show_path)
|
40
|
+
puts "File[ #{show_path} ] does not exist. creating file"
|
41
|
+
create_file show_path, '/ File created with NgOnRails view generator'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
def insert_render_views
|
48
|
+
if insert_the_render_views?
|
49
|
+
append_file index_path, render_index_view_template
|
50
|
+
append_file show_path, render_show_view_template
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def add_css
|
55
|
+
if options[:styles]
|
56
|
+
styles_path = "app/assets/stylesheets/ng_on_rails_styles.css"
|
57
|
+
unless File.exist?(styles_path)
|
58
|
+
puts "Adding ng_on_rails_styles.css -- <better with bootstrap and fontAwesome!>"
|
59
|
+
copy_file "#{ViewsGenerator.source_root}/styles_template.css", styles_path
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def class_name
|
67
|
+
@class_name ||= model_name.classify
|
68
|
+
end
|
69
|
+
|
70
|
+
def resource_name
|
71
|
+
@resource_name ||= class_name.underscore
|
72
|
+
end
|
73
|
+
|
74
|
+
def plural_name
|
75
|
+
@plural_name ||= resource_name.pluralize
|
76
|
+
end
|
77
|
+
|
78
|
+
def args arg_string
|
79
|
+
parts = arg_string.split("{")
|
80
|
+
if (parts.length > 1)
|
81
|
+
if !!parts[1].match(/required/)
|
82
|
+
required = true
|
83
|
+
else
|
84
|
+
required = false
|
85
|
+
end
|
86
|
+
if !!parts[1].match(/skip_form/)
|
87
|
+
skip_form = true
|
88
|
+
else
|
89
|
+
skip_form = false
|
90
|
+
end
|
91
|
+
if !!parts[1].match(/skip_index/)
|
92
|
+
skip_index = true
|
93
|
+
else
|
94
|
+
skip_index = false
|
95
|
+
end
|
96
|
+
if !!parts[1].match(/link/)
|
97
|
+
link = true
|
98
|
+
else
|
99
|
+
link = false
|
100
|
+
end
|
101
|
+
end
|
102
|
+
args = parts[0].split(":")
|
103
|
+
{
|
104
|
+
name: args[0],
|
105
|
+
type: args[1],
|
106
|
+
required: required,
|
107
|
+
skip_form: skip_form,
|
108
|
+
skip_index: skip_index,
|
109
|
+
link: link
|
110
|
+
}
|
111
|
+
end
|
112
|
+
|
113
|
+
#
|
114
|
+
# VIEW HELPERS
|
115
|
+
#
|
116
|
+
|
117
|
+
def type_to_class type_string
|
118
|
+
unless type_string.blank?
|
119
|
+
".#{type_string}"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def required_string property_hash
|
124
|
+
if property_hash[:required]
|
125
|
+
required_string = "required=\"true\""
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def insert_the_render_views?
|
130
|
+
!(options[:render_views].blank? || options[:render_views]=="false")
|
131
|
+
end
|
132
|
+
|
133
|
+
def is_plural? string
|
134
|
+
string.pluralize == string
|
135
|
+
end
|
136
|
+
|
137
|
+
def belongs_to_array
|
138
|
+
if options[:belongs_to].blank?
|
139
|
+
[]
|
140
|
+
else
|
141
|
+
options[:belongs_to].map do |a|
|
142
|
+
a.underscore.gsub(" ","")+".id"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def belongs_to_comma
|
148
|
+
unless options[:belongs_to].blank?
|
149
|
+
","
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def belongs_to_values
|
154
|
+
unless options[:belongs_to].blank?
|
155
|
+
belongs_to_array.join(",")
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
#
|
160
|
+
# TEMPLATES
|
161
|
+
#
|
162
|
+
|
163
|
+
def index_path
|
164
|
+
@index_path ||= "app/views/#{plural_name}/index.html.#{options[:format]}"
|
165
|
+
end
|
166
|
+
|
167
|
+
def show_path
|
168
|
+
@show_path ||= "app/views/#{plural_name}/show.html.#{options[:format]}"
|
169
|
+
end
|
170
|
+
|
171
|
+
def render_index_view_template
|
172
|
+
if options[:format]=="erb"
|
173
|
+
puts "ERB format not yet added"
|
174
|
+
elsif options[:format]=="haml"
|
175
|
+
puts "HAML format not yet added"
|
176
|
+
else
|
177
|
+
"
|
178
|
+
|
179
|
+
/
|
180
|
+
/ Inserted by NgOnRails view generator.
|
181
|
+
/
|
182
|
+
div ng-init=\"#{plural_name}=ctrl.rails.#{plural_name}\" render_view=\"true\" url=\"#{plural_name}/index\"
|
183
|
+
/
|
184
|
+
/
|
185
|
+
/
|
186
|
+
|
187
|
+
"
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def render_show_view_template
|
192
|
+
if options[:format]=="erb"
|
193
|
+
puts "ERB format not yet added"
|
194
|
+
elsif options[:format]=="haml"
|
195
|
+
puts "HAML format not yet added"
|
196
|
+
else
|
197
|
+
"
|
198
|
+
|
199
|
+
/
|
200
|
+
/ Inserted by NgOnRails view generator.
|
201
|
+
/
|
202
|
+
div ng-init=\"#{resource_name}=ctrl.rails.#{resource_name}\" render_view=\"true\" url=\"#{plural_name}/show\"
|
203
|
+
/
|
204
|
+
/
|
205
|
+
/
|
206
|
+
|
207
|
+
"
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
data/lib/ng_on_rails/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ng_on_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.3
|
4
|
+
version: 0.0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brookie Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: angularjs-rails
|
@@ -91,7 +91,7 @@ files:
|
|
91
91
|
- README.md
|
92
92
|
- Rakefile
|
93
93
|
- app/assets/javascripts/app.js
|
94
|
-
- app/assets/javascripts/directives/shared.js.
|
94
|
+
- app/assets/javascripts/directives/shared.js.coffee
|
95
95
|
- app/assets/javascripts/ng_on_rails.js
|
96
96
|
- app/controllers/ng_on_rails_controller.rb
|
97
97
|
- app/helpers/ng_on_rails_helper.rb
|
@@ -101,6 +101,13 @@ files:
|
|
101
101
|
- lib/generators/ng_on_rails/resource_generator.rb
|
102
102
|
- lib/generators/ng_on_rails/templates/controller_template.js.erb
|
103
103
|
- lib/generators/ng_on_rails/templates/resource_template.js.erb
|
104
|
+
- lib/generators/ng_on_rails/templates/styles_template.css
|
105
|
+
- lib/generators/ng_on_rails/templates/views/slim/_form.html.erb
|
106
|
+
- lib/generators/ng_on_rails/templates/views/slim/_model.html.erb
|
107
|
+
- lib/generators/ng_on_rails/templates/views/slim/_show.html.erb
|
108
|
+
- lib/generators/ng_on_rails/templates/views/slim/index.html.erb
|
109
|
+
- lib/generators/ng_on_rails/templates/views/slim/show.html.erb
|
110
|
+
- lib/generators/ng_on_rails/views_generator.rb
|
104
111
|
- lib/ng_on_rails.rb
|
105
112
|
- lib/ng_on_rails/engine.rb
|
106
113
|
- lib/ng_on_rails/version.rb
|
@@ -1,33 +0,0 @@
|
|
1
|
-
NgOnRailsApp.directive(
|
2
|
-
"renderView",
|
3
|
-
function(){
|
4
|
-
return {
|
5
|
-
restrict: "AE",
|
6
|
-
transclude: true,
|
7
|
-
template: function(el,attrs){
|
8
|
-
format = attrs.format || "html"
|
9
|
-
return '<div ng_include="\'/angular_app/'+attrs.url+'.'+format+'\'"></div>'
|
10
|
-
}
|
11
|
-
}
|
12
|
-
}
|
13
|
-
)
|
14
|
-
NgOnRailsApp.directive(
|
15
|
-
"render",
|
16
|
-
function(){
|
17
|
-
return {
|
18
|
-
restrict: "AE",
|
19
|
-
transclude: true,
|
20
|
-
template: function(el,attrs){
|
21
|
-
format = attrs.format || "html"
|
22
|
-
url_parts = attrs.url.split("/")
|
23
|
-
if (url_parts[url_parts.length-1].trim()==""){
|
24
|
-
url_parts.pop()
|
25
|
-
}
|
26
|
-
last = url_parts.pop()
|
27
|
-
url_parts.push("_"+last)
|
28
|
-
path = url_parts.join("/")
|
29
|
-
return '<div ng_include="\'/angular_app/'+path+'.'+format+'\'"></div>'
|
30
|
-
}
|
31
|
-
}
|
32
|
-
}
|
33
|
-
)
|