rails_lite 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +221 -5
- data/lib/rails_lite/version.rb +1 -1
- data/lib/scaffold/app/controllers/bands_controller.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbcf381a06732d23f274997af2d2e67977087cb6b59d773189e2ee766684f9dc
|
4
|
+
data.tar.gz: bc3332186e697520dc19a42dc922549222cbe6f7ec0926cd176299eb4ff00d10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 403808d65334e44514bd141b1a0982379425be91252cb36aa3e861ee3afdedb7b36ba611c793198701c573cc91311cca92d5e2186782c011b22935828223d5fb
|
7
|
+
data.tar.gz: ee7286b773cc84d8d532c1fcf809258f8f2c429b2a71226c9c5fb874c197d615bcc5039201dd3c7b52898db1cdea9c99a90f723a3883769364b0291ffb88241c
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# RailsLite
|
2
2
|
|
3
|
-
|
3
|
+
RailsLite is an MVC framework for building web applications. Some features include:
|
4
4
|
|
5
5
|
* SQLite or PostgreSQL ORM with associations and search
|
6
6
|
* Controllers with Session and Flash Management
|
@@ -33,13 +33,229 @@ Or install it yourself as:
|
|
33
33
|
|
34
34
|
## Usage
|
35
35
|
|
36
|
-
|
36
|
+
### Creating a Project
|
37
37
|
|
38
|
-
|
38
|
+
To create a new project navigate the directory you would like to create the project and run:
|
39
39
|
|
40
|
-
|
40
|
+
$ railslite new [PROJECT_NAME]
|
41
|
+
|
42
|
+
To start a local server run either:
|
43
|
+
|
44
|
+
$ railslite server
|
45
|
+
$ railslite s
|
46
|
+
|
47
|
+
To open pry for your project run either:
|
48
|
+
|
49
|
+
$ railslite console
|
50
|
+
$ railslite c
|
51
|
+
|
52
|
+
### Database
|
53
|
+
|
54
|
+
RailsLite's ORM works with SQLite, so you will need to edit the `db/database.sql` file to reflect your schema and include your seeds. Then run:
|
55
|
+
|
56
|
+
$ railslite dbreset
|
57
|
+
|
58
|
+
### Routes
|
59
|
+
|
60
|
+
Routes go in the `config/routes.rb` file. Routes work exactly like they do in rails.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
root to: 'bands#index'
|
64
|
+
|
65
|
+
resource :sessions, only: [:new, :create, :destroy]
|
66
|
+
|
67
|
+
resources :users, only: [:show, :new, :create] do
|
68
|
+
member do
|
69
|
+
get :[CUSTOM_ROUTE]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
resources :bands do
|
74
|
+
collection do
|
75
|
+
get :[CUSTOM_ROUTE]
|
76
|
+
end
|
77
|
+
resources :albums, only: [:new]
|
78
|
+
end
|
79
|
+
|
80
|
+
resources :albums, except: [:index, :new] do
|
81
|
+
resources :tracks, only: [:new]
|
82
|
+
end
|
83
|
+
|
84
|
+
resources :tracks, only: [:show, :create, :edit, :update, :destroy,]
|
85
|
+
|
86
|
+
resources :notes, only: [:create, :destroy]
|
87
|
+
patch '/[CUSTOM_ROUTE]/:id', to: 'notes#[CUSTOM_METHOD]'
|
88
|
+
get '/[CUSTOM_ROUTE]', to: 'notes#[CUSTOM_METHOD]'
|
89
|
+
```
|
90
|
+
|
91
|
+
URL helper methods are created for your routes. To view these methods and all of your routes run:
|
92
|
+
|
93
|
+
$ railslite routes
|
94
|
+
|
95
|
+
### Models
|
96
|
+
|
97
|
+
Models go in `app/models` and inherit from ApplicationModel. You can add methods to ApplicationModel at `app/models/application_model.rb`. Models have access to validations on presence, uniqueness and length, as well as, the lifecyle methods `after_initialize` and `before_validation`. Models also can have `belongs_to`, `has_many` and `has_one` using the same syntax as rails. Creating a `belongs_to` association will automatically create a validation for the presence of the `foreign_key` unless `optional: true` is included.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
require 'bcrypt'
|
101
|
+
|
102
|
+
class User < ApplicationModel
|
103
|
+
validates :username, presence: true, uniqueness: true
|
104
|
+
validates :password_digest, presence: true
|
105
|
+
validates :password, length: { minimum: 6, allow_nil: true }
|
106
|
+
validates :session_token, presence: true, uniqueness: true
|
107
|
+
|
108
|
+
has_many :notes
|
109
|
+
|
110
|
+
after_initialize :ensure_token
|
111
|
+
|
112
|
+
attr_reader :password
|
113
|
+
|
114
|
+
def self.find_by_credentials(username, password)
|
115
|
+
user = User.find_by(username: username)
|
116
|
+
user && user.is_password?(password) ? user : nil
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.generate_token
|
120
|
+
SecureRandom::urlsafe_base64
|
121
|
+
end
|
122
|
+
|
123
|
+
def password=(password)
|
124
|
+
@password = password
|
125
|
+
self.password_digest = BCrypt::Password.create(password)
|
126
|
+
end
|
127
|
+
|
128
|
+
def ensure_token
|
129
|
+
self.session_token ||= User.generate_token
|
130
|
+
end
|
131
|
+
|
132
|
+
def reset_token!
|
133
|
+
self.session_token = User.generate_token
|
134
|
+
self.save!
|
135
|
+
self.session_token
|
136
|
+
end
|
137
|
+
|
138
|
+
def is_password?(password)
|
139
|
+
BCrypt::Password.new(self.password_digest).is_password?(password)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
### Controllers
|
145
|
+
|
146
|
+
Models go in `app/controllers` and inherit from ApplicationController. You can add methods to ApplicationController at `app/controllers/application_model.rb`. Contoller methods will automatically render their corrosponding view if no render or redirect is specified. Controllers also support strong params and have a `before_action` lifecycle method. Including `protect_from_forgery` in a controller will implement CSRF protection. Include `<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">` in any of your forms when `protect_from_forgery` is enabled.
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
class BandsController < ApplicationController
|
150
|
+
protect_from_forgery
|
151
|
+
|
152
|
+
def index
|
153
|
+
@bands = Band.all
|
154
|
+
end
|
155
|
+
|
156
|
+
def show
|
157
|
+
@band = Band.find(params[:id])
|
158
|
+
end
|
159
|
+
|
160
|
+
def new
|
161
|
+
@band = Band.new
|
162
|
+
end
|
163
|
+
|
164
|
+
def create
|
165
|
+
@band = Band.new(band_params)
|
166
|
+
|
167
|
+
if @band.save
|
168
|
+
redirect_to band_url(@band)
|
169
|
+
else
|
170
|
+
flash.now[:errors] = @band.errors
|
171
|
+
render :new
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def edit
|
176
|
+
@band = Band.find(params[:id])
|
177
|
+
end
|
178
|
+
|
179
|
+
def update
|
180
|
+
@band = Band.find(params[:id])
|
181
|
+
if @band.update_attributes(band_params)
|
182
|
+
redirect_to band_url(@band)
|
183
|
+
else
|
184
|
+
flash.now[:errors] = @band.errors
|
185
|
+
render :edit
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def destroy
|
190
|
+
@band = Band.find(params[:id])
|
191
|
+
@band.destroy
|
192
|
+
redirect_to bands_url
|
193
|
+
end
|
194
|
+
|
195
|
+
def json
|
196
|
+
@bands = Band.all
|
197
|
+
end
|
198
|
+
|
199
|
+
private
|
200
|
+
|
201
|
+
def band_params
|
202
|
+
params.require(:band).permit(:name)
|
203
|
+
end
|
204
|
+
|
205
|
+
before_action :ensure_login
|
206
|
+
end
|
207
|
+
|
208
|
+
```
|
209
|
+
|
210
|
+
### Views
|
211
|
+
|
212
|
+
Create your views at `app/views/[CONTROLLER_NAME]/[ACTION].html.erb` or `app/views/[CONTROLLER_NAME]/[ACTION].json.jbuilder`. Place any code you want shared between all your HTML views in `app/views/application.html.erb`.
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
<h1 class='page-header'>Bands</h1>
|
216
|
+
|
217
|
+
<ul class='main-list'>
|
218
|
+
<% @bands.each do |band| %>
|
219
|
+
<li><a href="<%= band_url(band) %>"><p><%= band.name %></p></a></li>
|
220
|
+
<% end %>
|
221
|
+
</ul>
|
222
|
+
|
223
|
+
<h4 class='sub-header'>Links</h4>
|
224
|
+
<ul class='page-links'>
|
225
|
+
<li><a class='button' href="<%= new_bands_url %>" >New band</a></li>
|
226
|
+
</ul>
|
227
|
+
```
|
228
|
+
```ruby
|
229
|
+
Jbuilder.encode do |json|
|
230
|
+
json.array! @bands
|
231
|
+
end
|
232
|
+
```
|
233
|
+
|
234
|
+
### Relation
|
235
|
+
|
236
|
+
RailsLite includes a `Relation` class that searches the database with SQL queiries similar to active record. The Relation methods include:
|
237
|
+
|
238
|
+
* `where`
|
239
|
+
* `joins`
|
240
|
+
* `left_joins`
|
241
|
+
* `select`
|
242
|
+
* `find_by`
|
243
|
+
* `find`
|
244
|
+
* `first`
|
245
|
+
* `last`
|
246
|
+
* `all`
|
247
|
+
* `save`
|
248
|
+
* `save!`
|
249
|
+
* `update`
|
250
|
+
* `update_attributes`
|
251
|
+
* `destroy`
|
252
|
+
|
253
|
+
Relation methods are lazy and stackable. For example:
|
254
|
+
|
255
|
+
```ruby
|
256
|
+
User.joins(:bands).where(band_id: 1)
|
257
|
+
```
|
41
258
|
|
42
|
-
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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
43
259
|
|
44
260
|
## Contributing
|
45
261
|
|
data/lib/rails_lite/version.rb
CHANGED
@@ -22,7 +22,7 @@ class BandsController < ApplicationController
|
|
22
22
|
if @band.save
|
23
23
|
redirect_to band_url(@band)
|
24
24
|
else
|
25
|
-
flash.now[:errors] = @band.errors
|
25
|
+
flash.now[:errors] = @band.errors
|
26
26
|
render :new
|
27
27
|
end
|
28
28
|
end
|
@@ -37,7 +37,7 @@ class BandsController < ApplicationController
|
|
37
37
|
if @band.update_attributes(band_params)
|
38
38
|
redirect_to band_url(@band)
|
39
39
|
else
|
40
|
-
flash.now[:errors] = @band.errors
|
40
|
+
flash.now[:errors] = @band.errors
|
41
41
|
render :edit
|
42
42
|
end
|
43
43
|
end
|