spine-rails 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +121 -0
- data/app/assets/javascripts/spine.js +8 -10
- data/app/assets/javascripts/spine/ajax.js +14 -9
- data/app/assets/javascripts/spine/rails.js +3 -0
- data/lib/generators/spine/controller/controller_generator.rb +15 -0
- data/lib/generators/spine/controller/templates/controller.coffee.erb +10 -0
- data/lib/generators/spine/model/model_generator.rb +17 -0
- data/lib/generators/spine/model/templates/model.coffee.erb +3 -0
- data/lib/generators/spine/new/new_generator.rb +45 -0
- data/lib/generators/spine/new/templates/index.coffee.erb +25 -0
- data/lib/generators/spine/view/templates/view.eco.erb +2 -0
- data/lib/generators/spine/view/templates/view.ejs.erb +2 -0
- data/lib/generators/spine/view/templates/view.hamljs.erb +2 -0
- data/lib/generators/spine/view/view_generator.rb +24 -0
- data/lib/spine/generators.rb +19 -0
- data/lib/spine/rails/version.rb +2 -2
- data/spine-rails.gemspec +3 -4
- metadata +23 -22
- data/lib/generators/spine/install/install_generator.rb +0 -18
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
# Spine-Rails
|
2
|
+
|
3
|
+
Easily setup and use [Spine](http://spinejs.com) with Rails 3.1.
|
4
|
+
|
5
|
+
## Rails 3.1 setup
|
6
|
+
|
7
|
+
This gem requires the use of [Rails 3.1](http://rubyonrails.org), [CoffeeScript](http://jashkenas.github.com/coffee-script/) and the new Rails asset pipeline provided by [Sprockets](http://getsprockets.org).
|
8
|
+
|
9
|
+
This gem does two things:
|
10
|
+
|
11
|
+
* Adds Spine to the asset pipeline, so you can easily require it in your applications: `//= require spine`
|
12
|
+
|
13
|
+
* Adds some Spine generators, so you can easily create Spine Models, Views and Controllers.
|
14
|
+
|
15
|
+
For versions of Rails less than 3.1, it only provides the generator to install JavaScript file into public directory.
|
16
|
+
|
17
|
+
### Installation
|
18
|
+
|
19
|
+
In your Gemfile, add this line:
|
20
|
+
|
21
|
+
gem "spine-rails"
|
22
|
+
|
23
|
+
Then run the following commands:
|
24
|
+
|
25
|
+
bundle install
|
26
|
+
|
27
|
+
rails generate spine:new
|
28
|
+
|
29
|
+
### Layout and namespacing
|
30
|
+
|
31
|
+
Running `rails g spine:new` will create the following directory structure:
|
32
|
+
|
33
|
+
app/assets/javascripts/app/models/
|
34
|
+
app/assets/javascripts/app/views/
|
35
|
+
app/assets/javascripts/app/controllers/
|
36
|
+
app/assets/javascripts/app/index.coffee
|
37
|
+
|
38
|
+
By default your application will be namespaced by the `app` directory. You can specify a different namespace with the `--app` option:
|
39
|
+
|
40
|
+
rails g spine:new --app foo_bar
|
41
|
+
|
42
|
+
Use the top-level level `index.coffee` file to setup namespacing and initial controller instantiation.
|
43
|
+
|
44
|
+
## Generators
|
45
|
+
|
46
|
+
spine-rails provides three simple generators to help you get started:
|
47
|
+
|
48
|
+
### Model
|
49
|
+
|
50
|
+
rails g spine:model User email username full_name
|
51
|
+
|
52
|
+
This generator creates a very minimal model inside `app/assets/javascript/models`. You can optionally provide a list of attributes for the model.
|
53
|
+
|
54
|
+
### Controller
|
55
|
+
|
56
|
+
rails g spine:controller Users
|
57
|
+
|
58
|
+
This generator creates a minimal `Users` controller in `app/assets/javascripts/controllers` to get you started.
|
59
|
+
|
60
|
+
### View
|
61
|
+
|
62
|
+
rails g spine:view users/index
|
63
|
+
|
64
|
+
This generates creates blank Spine views in the `app/assets/javascripts/views/users` directory.
|
65
|
+
|
66
|
+
The generator will create views in `hamljs`, `eco` or `ejs` format, depending on which gems are availale:
|
67
|
+
|
68
|
+
- [eco](https://github.com/sstephenson/eco) - will use ECO templates
|
69
|
+
- [rub-haml-js](https://github.com/dnagir/ruby-haml-js) - will use HAMLJS templates
|
70
|
+
- otherwise, EJS templates will be used
|
71
|
+
|
72
|
+
## Example Usage
|
73
|
+
|
74
|
+
Created a new Rails 3.1 application called `blog`.
|
75
|
+
|
76
|
+
rails new blog
|
77
|
+
|
78
|
+
Edit your Gemfile and add
|
79
|
+
|
80
|
+
gem 'spine-rails'
|
81
|
+
|
82
|
+
Install the gem and generate resource.
|
83
|
+
|
84
|
+
bundle install
|
85
|
+
|
86
|
+
rails g scaffold Post title:string content:string
|
87
|
+
rake db:migrate
|
88
|
+
|
89
|
+
rails g spine:new
|
90
|
+
rails g spine:model Post title content
|
91
|
+
rails g spine:controllers Posts
|
92
|
+
|
93
|
+
You now have the default Spine data structures available to work with.
|
94
|
+
|
95
|
+
Next navigate to [http://localhost:3000/posts](http://localhost:3000/posts), and open up the JavaScript console in the browser.
|
96
|
+
|
97
|
+
Now you can use Spine:
|
98
|
+
|
99
|
+
// Sends an AJAX POST to the server
|
100
|
+
var post = App.Post.create({
|
101
|
+
title: 'Hello World!',
|
102
|
+
content: 'Spine & Rails, sitting in a tree!'
|
103
|
+
});
|
104
|
+
|
105
|
+
// => ID returned from Rails
|
106
|
+
post.id;
|
107
|
+
|
108
|
+
// Sends AJAX PUT to the server
|
109
|
+
post.updateAttributes({title: 'Goodbye'});
|
110
|
+
|
111
|
+
Reload the page, then:
|
112
|
+
|
113
|
+
App.Post.fetch(); // Fetch all posts
|
114
|
+
|
115
|
+
App.Post.first().content;
|
116
|
+
|
117
|
+
For more information on how to integrate Spine with Rails, please see the [Rails guide](http://spinejs.com/docs/rails).
|
118
|
+
|
119
|
+
## Attributions
|
120
|
+
|
121
|
+
This plugin was made by [Alex MacCaw](http://alexmaccaw.co.uk) with major contributions from [Dmytrii Nagirniak](https://github.com/dnagir). It's under the same license as [Spine](http://spinejs.com) (MIT).
|
@@ -395,20 +395,16 @@
|
|
395
395
|
return rec && rec.constructor === this.constructor && (rec.id === this.id || (_ref = this.id, __indexOf.call(rec.ids, _ref) >= 0) || (_ref2 = rec.id, __indexOf.call(this.ids, _ref2) >= 0));
|
396
396
|
};
|
397
397
|
Model.prototype.save = function() {
|
398
|
-
var error;
|
398
|
+
var error, record;
|
399
399
|
error = this.validate();
|
400
400
|
if (error) {
|
401
401
|
this.trigger('error', error);
|
402
402
|
return false;
|
403
403
|
}
|
404
404
|
this.trigger('beforeSave');
|
405
|
-
|
406
|
-
this.create();
|
407
|
-
} else {
|
408
|
-
this.update();
|
409
|
-
}
|
405
|
+
record = this.newRecord ? this.create() : this.update();
|
410
406
|
this.trigger('save');
|
411
|
-
return
|
407
|
+
return record;
|
412
408
|
};
|
413
409
|
Model.prototype.updateAttribute = function(name, value) {
|
414
410
|
this[name] = value;
|
@@ -474,7 +470,8 @@
|
|
474
470
|
records[this.id].load(this.attributes());
|
475
471
|
clone = records[this.id].clone();
|
476
472
|
clone.trigger('update');
|
477
|
-
|
473
|
+
clone.trigger('change', 'update');
|
474
|
+
return clone;
|
478
475
|
};
|
479
476
|
Model.prototype.create = function() {
|
480
477
|
var clone, records;
|
@@ -487,7 +484,8 @@
|
|
487
484
|
records[this.id] = this.dup(false);
|
488
485
|
clone = records[this.id].clone();
|
489
486
|
clone.trigger('create');
|
490
|
-
|
487
|
+
clone.trigger('change', 'create');
|
488
|
+
return clone;
|
491
489
|
};
|
492
490
|
Model.prototype.bind = function(events, callback) {
|
493
491
|
var binder, unbinder;
|
@@ -684,7 +682,7 @@
|
|
684
682
|
if (typeof module !== "undefined" && module !== null) {
|
685
683
|
module.exports = Spine;
|
686
684
|
}
|
687
|
-
Spine.version = '1.0.
|
685
|
+
Spine.version = '1.0.3';
|
688
686
|
Spine.isArray = isArray;
|
689
687
|
Spine.isBlank = isBlank;
|
690
688
|
Spine.$ = $;
|
@@ -57,7 +57,10 @@
|
|
57
57
|
Base.prototype.defaults = {
|
58
58
|
contentType: 'application/json',
|
59
59
|
dataType: 'json',
|
60
|
-
processData: false
|
60
|
+
processData: false,
|
61
|
+
headers: {
|
62
|
+
'X-Requested-With': 'XMLHttpRequest'
|
63
|
+
}
|
61
64
|
};
|
62
65
|
Base.prototype.ajax = function(params, defaults) {
|
63
66
|
return $.ajax($.extend({}, this.defaults, defaults, params));
|
@@ -203,15 +206,17 @@
|
|
203
206
|
};
|
204
207
|
Model.Ajax = {
|
205
208
|
extended: function() {
|
206
|
-
this.
|
207
|
-
|
208
|
-
});
|
209
|
-
this.fetch(function() {
|
210
|
-
var _ref;
|
211
|
-
return (_ref = this.ajax()).fetch.apply(_ref, arguments);
|
212
|
-
});
|
209
|
+
this.fetch(this.ajaxFetch);
|
210
|
+
this.change(this.ajaxChange);
|
213
211
|
this.extend(Extend);
|
214
212
|
return this.include(Include);
|
213
|
+
},
|
214
|
+
ajaxFetch: function() {
|
215
|
+
var _ref;
|
216
|
+
return (_ref = this.ajax()).fetch.apply(_ref, arguments);
|
217
|
+
},
|
218
|
+
ajaxChange: function(record, type) {
|
219
|
+
return record.ajax()[type]();
|
215
220
|
}
|
216
221
|
};
|
217
222
|
Model.Ajax.Methods = {
|
@@ -224,4 +229,4 @@
|
|
224
229
|
if (typeof module !== "undefined" && module !== null) {
|
225
230
|
module.exports = Ajax;
|
226
231
|
}
|
227
|
-
}).call(this);
|
232
|
+
}).call(this);
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'spine/generators'
|
3
|
+
|
4
|
+
module Spine
|
5
|
+
module Generators
|
6
|
+
class ControllerGenerator < Base
|
7
|
+
source_root File.expand_path("../templates", __FILE__)
|
8
|
+
desc "Generate a Spine controller"
|
9
|
+
|
10
|
+
def create_controller
|
11
|
+
template "controller.coffee.erb", "app/assets/javascripts/#{app_name}/controllers/#{file_name}.coffee"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'spine/generators'
|
3
|
+
|
4
|
+
module Spine
|
5
|
+
module Generators
|
6
|
+
class ModelGenerator < Base
|
7
|
+
source_root File.expand_path("../templates", __FILE__)
|
8
|
+
desc "Generate a Spine model with configured fields"
|
9
|
+
|
10
|
+
argument :fields, :desc => 'List of model attributes', :type => :array, :banner => 'field1 field2'
|
11
|
+
|
12
|
+
def create_model
|
13
|
+
template "model.coffee.erb", "app/assets/javascripts/#{app_name}/models/#{file_name}.coffee"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module Spine
|
4
|
+
module Generators
|
5
|
+
class NewGenerator < ::Rails::Generators::Base
|
6
|
+
source_root File.expand_path("../templates", __FILE__)
|
7
|
+
|
8
|
+
desc "This generator installs Spine #{Spine::Rails::SPINE_VERSION} as part of assets pipeline"
|
9
|
+
|
10
|
+
class_option :app, :type => :string, :default => "app", :desc => "app name"
|
11
|
+
|
12
|
+
def app_name
|
13
|
+
options[:app]
|
14
|
+
end
|
15
|
+
|
16
|
+
def app_class
|
17
|
+
app_name.camelize
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_dir_layout
|
21
|
+
%W{models views controllers lib}.each do |dir|
|
22
|
+
empty_directory "app/assets/javascripts/#{app_name}/#{dir}"
|
23
|
+
create_file "app/assets/javascripts/#{app_name}/#{dir}/.gitkeep"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_app_file
|
28
|
+
template "index.coffee.erb", "app/assets/javascripts/#{app_name}/index.coffee"
|
29
|
+
end
|
30
|
+
|
31
|
+
def add_spine_app_to_application
|
32
|
+
source = "app/assets/javascripts/application.js"
|
33
|
+
content = File.read(source)
|
34
|
+
|
35
|
+
if content.include?("//= require_tree .")
|
36
|
+
inject_into_file source, :before => "//= require_tree ." do
|
37
|
+
"//= require #{app_name}\n"
|
38
|
+
end
|
39
|
+
else
|
40
|
+
append_file source, "\n//= require #{app_name}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#= require json2
|
2
|
+
#= require jquery
|
3
|
+
#= require spine
|
4
|
+
#= require spine/manager
|
5
|
+
#= require spine/ajax
|
6
|
+
#= require spine/route
|
7
|
+
#= require spine/rails
|
8
|
+
|
9
|
+
#= require_tree ./lib
|
10
|
+
#= require_self
|
11
|
+
#= require_tree ./models
|
12
|
+
#= require_tree ./controllers
|
13
|
+
#= require_tree ./views
|
14
|
+
|
15
|
+
class <%= app_class %> extends Spine.Controller
|
16
|
+
constructor: ->
|
17
|
+
super
|
18
|
+
|
19
|
+
# Initialize controllers:
|
20
|
+
# @append(@items = new <%= app_class %>.Items)
|
21
|
+
# ...
|
22
|
+
|
23
|
+
Spine.Route.setup()
|
24
|
+
|
25
|
+
window.<%= app_class %> = <%= app_class %>
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'spine/generators'
|
3
|
+
|
4
|
+
module Spine
|
5
|
+
module Generators
|
6
|
+
class ViewGenerator < Base
|
7
|
+
source_root File.expand_path("../templates", __FILE__)
|
8
|
+
desc "Generate a Spine view, eg: posts/index"
|
9
|
+
|
10
|
+
# rails g spine:view Users index edit show
|
11
|
+
def create_views
|
12
|
+
view_format = if defined?(::Eco)
|
13
|
+
'eco'
|
14
|
+
elsif defined?(::RubyHamlJs)
|
15
|
+
'hamljs'
|
16
|
+
else
|
17
|
+
'ejs'
|
18
|
+
end
|
19
|
+
|
20
|
+
template "view.#{view_format}.erb", "app/assets/javascripts/#{app_name}/views/#{file_name}.jst.#{view_format}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Spine
|
2
|
+
module Generators
|
3
|
+
class Base < ::Rails::Generators::NamedBase
|
4
|
+
class_option :app, :type => :string, :default => "app", :desc => "app name"
|
5
|
+
|
6
|
+
def class_name
|
7
|
+
(class_path + [file_name]).map!{ |m| m.camelize }.join('')
|
8
|
+
end
|
9
|
+
|
10
|
+
def app_name
|
11
|
+
options[:app]
|
12
|
+
end
|
13
|
+
|
14
|
+
def app_class
|
15
|
+
app_name.camelize
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/spine/rails/version.rb
CHANGED
data/spine-rails.gemspec
CHANGED
@@ -14,10 +14,9 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.required_rubygems_version = ">= 1.3.6"
|
15
15
|
s.rubyforge_project = "spine-rails"
|
16
16
|
|
17
|
-
s.add_dependency
|
18
|
-
s.add_development_dependency "bundler"
|
19
|
-
|
20
|
-
|
17
|
+
s.add_dependency "rails", ">= 3.1.0"
|
18
|
+
s.add_development_dependency "bundler"
|
19
|
+
|
21
20
|
s.files = `git ls-files`.split("\n")
|
22
21
|
s.executables = `git ls-files`.split("\n").select{|f| f =~ /^bin/}
|
23
22
|
s.require_path = 'lib'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spine-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,41 +9,30 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
12
|
+
date: 2011-10-21 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement: &
|
15
|
+
name: rails
|
16
|
+
requirement: &70146319394780 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: 3.1.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70146319394780
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
28
|
-
none: false
|
29
|
-
requirements:
|
30
|
-
- - ~>
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: 1.0.0
|
33
|
-
type: :development
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *70334460548120
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: rails
|
38
|
-
requirement: &70334460547660 !ruby/object:Gem::Requirement
|
27
|
+
requirement: &70146319394400 !ruby/object:Gem::Requirement
|
39
28
|
none: false
|
40
29
|
requirements:
|
41
|
-
- -
|
30
|
+
- - ! '>='
|
42
31
|
- !ruby/object:Gem::Version
|
43
|
-
version: '
|
32
|
+
version: '0'
|
44
33
|
type: :development
|
45
34
|
prerelease: false
|
46
|
-
version_requirements: *
|
35
|
+
version_requirements: *70146319394400
|
47
36
|
description: This gem provides Spine for your Rails 3 application.
|
48
37
|
email:
|
49
38
|
- info@eribium.org
|
@@ -54,6 +43,7 @@ files:
|
|
54
43
|
- .gitignore
|
55
44
|
- Gemfile
|
56
45
|
- Gemfile.lock
|
46
|
+
- README.md
|
57
47
|
- Rakefile
|
58
48
|
- app/assets/javascripts/json2.js
|
59
49
|
- app/assets/javascripts/spine.js
|
@@ -61,12 +51,23 @@ files:
|
|
61
51
|
- app/assets/javascripts/spine/list.js
|
62
52
|
- app/assets/javascripts/spine/local.js
|
63
53
|
- app/assets/javascripts/spine/manager.js
|
54
|
+
- app/assets/javascripts/spine/rails.js
|
64
55
|
- app/assets/javascripts/spine/relation.js
|
65
56
|
- app/assets/javascripts/spine/route.js
|
66
57
|
- app/assets/javascripts/spine/tabs.js
|
67
58
|
- app/assets/javascripts/spine/tmpl.js
|
68
|
-
- lib/generators/spine/
|
59
|
+
- lib/generators/spine/controller/controller_generator.rb
|
60
|
+
- lib/generators/spine/controller/templates/controller.coffee.erb
|
61
|
+
- lib/generators/spine/model/model_generator.rb
|
62
|
+
- lib/generators/spine/model/templates/model.coffee.erb
|
63
|
+
- lib/generators/spine/new/new_generator.rb
|
64
|
+
- lib/generators/spine/new/templates/index.coffee.erb
|
65
|
+
- lib/generators/spine/view/templates/view.eco.erb
|
66
|
+
- lib/generators/spine/view/templates/view.ejs.erb
|
67
|
+
- lib/generators/spine/view/templates/view.hamljs.erb
|
68
|
+
- lib/generators/spine/view/view_generator.rb
|
69
69
|
- lib/spine-rails.rb
|
70
|
+
- lib/spine/generators.rb
|
70
71
|
- lib/spine/rails.rb
|
71
72
|
- lib/spine/rails/engine.rb
|
72
73
|
- lib/spine/rails/version.rb
|
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'rails'
|
2
|
-
|
3
|
-
module Spine
|
4
|
-
module Generators
|
5
|
-
class InstallGenerator < ::Rails::Generators::Base
|
6
|
-
|
7
|
-
desc "This generator installs Spine #{Spine::Rails::SPINE_VERSION}"
|
8
|
-
|
9
|
-
source_root File.expand_path('../../../../../app/assets/javascripts', __FILE__)
|
10
|
-
|
11
|
-
def copy_spine
|
12
|
-
say_status("copying", "Spine (#{Spine::Rails::SPINE_VERSION})", :green)
|
13
|
-
copy_file "spine.js", "public/javascripts/spine.js"
|
14
|
-
copy_file "spine.min.js", "public/javascripts/spine.min.js"
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end if ::Rails.version < "3.1"
|