Skip to content

Commit 8dcb309

Browse files
author
Rodik
committed
First commit, first version
0 parents  commit 8dcb309

30 files changed

+382
-0
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/.bundle/
2+
/.yardoc
3+
/Gemfile.lock
4+
/_yardoc/
5+
/coverage/
6+
/doc/
7+
/pkg/
8+
/spec/reports/
9+
/tmp/

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--format documentation
2+
--color

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: ruby
2+
rvm:
3+
- 2.2.1

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in spa_rails.gemspec
4+
gemspec

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# SpaRails
2+
3+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/spa_rails`. To experiment with that code, run `bin/console` for an interactive prompt.
4+
5+
TODO: Delete this and the text above, and describe your gem
6+
7+
## Installation
8+
9+
Add this line to your application's Gemfile:
10+
11+
```ruby
12+
gem 'spa_rails'
13+
```
14+
15+
And then execute:
16+
17+
$ bundle
18+
19+
Or install it yourself as:
20+
21+
$ gem install spa_rails
22+
23+
## Usage
24+
25+
TODO: Write usage instructions here
26+
27+
## Development
28+
29+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30+
31+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32+
33+
## Contributing
34+
35+
1. Fork it ( https://github.com/[my-github-username]/spa_rails/fork )
36+
2. Create your feature branch (`git checkout -b my-new-feature`)
37+
3. Commit your changes (`git commit -am 'Add some feature'`)
38+
4. Push to the branch (`git push origin my-new-feature`)
39+
5. Create a new Pull Request

Rakefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require "bundler/gem_tasks"
2+

bin/console

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "spa_rails"
5+
6+
# You can add fixtures and/or initialization code here to make experimenting
7+
# with your gem easier. You can also use a different console, if you like.
8+
9+
# (If you use this, don't forget to add pry to your Gemfile!)
10+
# require "pry"
11+
# Pry.start
12+
13+
require "irb"
14+
IRB.start

bin/setup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
5+
bundle install
6+
7+
# Do any other automated setup that you need to do here

lib/generators/spa_rails_generator.rb

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
require 'rails/generators/base'
2+
require 'securerandom'
3+
4+
module SpaRails
5+
module Generators
6+
class SpaRailsGenerator < Rails::Generators::Base
7+
namespace "spa_rails"
8+
9+
desc "Install SPA in rails"
10+
11+
source_root File.expand_path("../templates", __FILE__)
12+
13+
def add_routes
14+
routes = []
15+
routes << %Q|get '/api/posts', to: proc { [200, {"Content-Type" => "application/json"}, ['{"resources": [{"title": "Title1"}, {"title": "Title2"}]}']] }|
16+
routes << "match '/(*path)', via: :all, to: frontend_page('index.htm')"
17+
18+
log :route, "Add SPA routes"
19+
20+
in_root do
21+
routes.each do |route|
22+
inject_into_file 'config/routes.rb', " #{route}\n", { before: /end\Z/, verbose: false }
23+
end
24+
end
25+
end
26+
27+
def add_configs
28+
configs = [
29+
"config.assets.version = '1.0'",
30+
"config.assets.compile = true",
31+
"config.assets.digest = false",
32+
"config.assets.compress = false",
33+
"config.assets.debug = true"
34+
]
35+
36+
environment do
37+
"config.angular_templates.inside_paths << Rails.root.join('frontend')"
38+
end
39+
40+
environment(nil, env: "development") do
41+
configs.join("\n ")
42+
end
43+
end
44+
45+
def add_dependencies
46+
gem 'angular-rails-templates', github: 'sars/angular-rails-templates'
47+
48+
log :gemfile, "rails-assets gems"
49+
50+
in_root do
51+
append_file "Gemfile", "\nsource 'https://rails-assets.org' do", force: true
52+
53+
@in_group = true
54+
55+
gem 'rails-assets-angular'
56+
gem 'rails-assets-ui-utils'
57+
gem 'rails-assets-ui-router'
58+
gem 'rails-assets-restangular', '1.4.0'
59+
gem 'rails-assets-lodash'
60+
gem 'rails-assets-angular-bootstrap'
61+
gem 'rails-assets-bootstrap'
62+
63+
@in_group = false
64+
65+
append_file "Gemfile", "\nend\n", force: true
66+
end
67+
end
68+
69+
def copy_frontend
70+
directory "frontend", "frontend"
71+
end
72+
end
73+
end
74+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
/* Controllers */
4+
5+
angular.module('app').controller('AppCtrl', function($scope) {
6+
$scope.data = 'App data';
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1
2+
'{{data}}
3+
.jumbotron(ui-view='')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
body {
2+
min-height: 2000px;
3+
}
4+
5+
.post-container {
6+
margin: 10px 20% 10px 20%;
7+
background-color: #C3BFC1;
8+
padding: 20px;
9+
}
10+
11+
h1 {
12+
text-align: center;
13+
}
14+
15+
h3 {
16+
margin: 0 0 20px 0;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
angular.module('app').controller('PostsCtrl', function($scope, $location, Restangular) {
2+
$scope.posts = [];
3+
loadPosts();
4+
5+
// GET posts from server. Send app instance to server.
6+
function loadPosts() {
7+
Restangular.all('posts').getList().then(function(posts) {
8+
$scope.posts = posts;
9+
});
10+
}
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
h2 Post list
2+
div(ng-repeat="post in posts")
3+
.post-container
4+
h3.title
5+
'{{post.title}}
6+
span.glyphicon.glyphicon-search
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
/**
4+
* Config for the restangular
5+
*/
6+
angular.module('app')
7+
.config(function(RestangularProvider) {
8+
RestangularProvider.setBaseUrl('/api');
9+
10+
11+
RestangularProvider.addResponseInterceptor(function (response, operation, what) {
12+
if (operation === 'getList') {
13+
if (response['resources']) response['resources'].count = response.count;
14+
return response['resources'];
15+
}
16+
if (['get', 'post', 'put'].indexOf(operation) !== -1) {
17+
return response['resource'];
18+
}
19+
return response;
20+
});
21+
22+
RestangularProvider.addRequestInterceptor(function (request, operation, what) {
23+
if (operation === 'post' || operation === 'put') {
24+
return {resource: request};
25+
}
26+
return request;
27+
});
28+
29+
RestangularProvider.setDefaultHeaders({'Content-Type': 'application/json'});
30+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
/**
4+
* Config for the router
5+
*/
6+
angular.module('app')
7+
.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
8+
$locationProvider.html5Mode(true);
9+
10+
$stateProvider
11+
.state('app', {
12+
abstract: true,
13+
controller: 'AppCtrl',
14+
templateUrl: 'components/app/app.html'
15+
})
16+
.state('app.posts', {
17+
url: '/posts',
18+
controller: 'PostsCtrl',
19+
templateUrl: 'components/posts/index.html'
20+
});
21+
22+
$urlRouterProvider.otherwise('/');
23+
24+
});

lib/generators/templates/frontend/dirrectives/.keep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
*= require bootstrap
3+
*= require_tree ../
4+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
*= require lodash
3+
*= require angular
4+
*= require ui-utils
5+
*= require ui-router
6+
*= require angular-bootstrap
7+
*= require restangular
8+
*= require angular-rails-templates
9+
*= require ../module
10+
*= require_tree ../
11+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
angular.module('app', [
4+
'ui.jq',
5+
'templates',
6+
'ui.router',
7+
'restangular',
8+
'ui.bootstrap'
9+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
doctype html
2+
html(lang='en' data-ng-app='app')
3+
head
4+
base(href="/")
5+
meta(charset='utf-8')
6+
title App name
7+
meta(name='description' content='App description')
8+
meta(charset='utf-8')
9+
= stylesheet_link_tag 'manifests/application'
10+
11+
body
12+
13+
#app.app(ui-view='')
14+
= javascript_include_tag 'manifests/application'

lib/generators/templates/frontend/services/.keep

Whitespace-only changes.

lib/spa_rails.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "spa_rails/version"
2+
require "spa_rails/routes"
3+
require "spa_rails/rails"
4+
require "spa_rails/engine"
5+
6+
module SpaRails
7+
end

lib/spa_rails/engine.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "slim"
2+
3+
module SpaRails
4+
class Engine < ::Rails::Engine
5+
initializer 'spa-rails', group: :all do |app|
6+
app.assets.register_engine('.slimpage', Slim::Template)
7+
end
8+
end
9+
end

lib/spa_rails/rails.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module SpaRails
2+
class Engine < ::Rails::Engine
3+
config.assets.paths << find_root('.').join("frontend")
4+
5+
config.assets.precompile << lambda do |filename, path|
6+
path =~ /frontend/ && !%w(.js .css).include?(File.extname(filename))
7+
end
8+
end
9+
end

lib/spa_rails/routes.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module ActionDispatch::Routing
2+
class Mapper
3+
def frontend_page(page_name)
4+
Rails.application.assets.dup.instance_eval do
5+
@context_class.instance_eval do
6+
config = Rails.application.config.assets
7+
self.debug_assets = config.debug
8+
self.digest_assets = config.digest
9+
end
10+
11+
define_singleton_method :call do |env|
12+
path = "pages/#{page_name}"
13+
# Look up the asset.
14+
asset = find_asset(path, :bundle => !body_only?(env))
15+
ok_response(asset, env)
16+
end
17+
18+
self
19+
end
20+
end
21+
end
22+
end

lib/spa_rails/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module SpaRails
2+
VERSION = "0.1.0"
3+
end

spa_rails.gemspec

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'spa_rails/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "spa_rails"
8+
spec.version = SpaRails::VERSION
9+
spec.authors = ["Rodik"]
10+
spec.email = ["[email protected]"]
11+
12+
spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
13+
spec.description = %q{TODO: Write a longer description or delete this line.}
14+
spec.homepage = "TODO: Put your gem's website or public repo URL here."
15+
16+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17+
spec.bindir = "exe"
18+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19+
spec.require_paths = ["lib"]
20+
21+
if spec.respond_to?(:metadata)
22+
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
23+
end
24+
25+
spec.add_development_dependency "bundler", "~> 1.8"
26+
spec.add_development_dependency "rake", "~> 10.0"
27+
spec.add_dependency "slim", "~> 3.0.3"
28+
spec.add_dependency "sass-rails"
29+
end

0 commit comments

Comments
 (0)