sinatra-admin 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +176 -0
- data/Rakefile +2 -0
- data/dummy/config.ru +12 -0
- data/dummy/config/mongoid.yml +6 -0
- data/dummy/dummy.rb +19 -0
- data/dummy/models/admin.rb +6 -0
- data/dummy/models/comment.rb +5 -0
- data/dummy/models/user.rb +11 -0
- data/dummy/views/admin/customs/index.haml +4 -0
- data/dummy/views/admin/users/custom.haml +2 -0
- data/features/admin_login.feature +36 -0
- data/features/admin_logout.feature +14 -0
- data/features/creating_users.feature +44 -0
- data/features/custom_pages.feature +34 -0
- data/features/default_root.feature +43 -0
- data/features/editing_users.feature +46 -0
- data/features/listing_users.feature +46 -0
- data/features/main_menu_resources.feature +41 -0
- data/features/removing_users.feature +34 -0
- data/features/step_definitions/common_steps.rb +59 -0
- data/features/step_definitions/web_steps.rb +212 -0
- data/features/support/database_cleaner.rb +17 -0
- data/features/support/env.rb +18 -0
- data/features/support/paths.rb +30 -0
- data/features/support/sinatra_admin.rb +3 -0
- data/features/support/warden.rb +9 -0
- data/features/user_details.feature +31 -0
- data/lib/sinatra-admin.rb +42 -0
- data/lib/sinatra-admin/app.rb +74 -0
- data/lib/sinatra-admin/config.rb +45 -0
- data/lib/sinatra-admin/helpers/session.rb +24 -0
- data/lib/sinatra-admin/helpers/template_lookup.rb +7 -0
- data/lib/sinatra-admin/models/admin.rb +40 -0
- data/lib/sinatra-admin/register.rb +8 -0
- data/lib/sinatra-admin/register/base.rb +29 -0
- data/lib/sinatra-admin/register/custom.rb +10 -0
- data/lib/sinatra-admin/register/model.rb +75 -0
- data/lib/sinatra-admin/version.rb +3 -0
- data/lib/sinatra-admin/views/auth/login.haml +16 -0
- data/lib/sinatra-admin/views/edit.haml +21 -0
- data/lib/sinatra-admin/views/index.haml +34 -0
- data/lib/sinatra-admin/views/layout.haml +19 -0
- data/lib/sinatra-admin/views/new.haml +19 -0
- data/lib/sinatra-admin/views/show.haml +16 -0
- data/lib/sinatra-admin/warden_strategies/sinatra_admin.rb +27 -0
- data/sinatra-admin.gemspec +36 -0
- data/spec/sinatra-admin/app_spec.rb +15 -0
- data/spec/sinatra-admin/config_spec.rb +111 -0
- data/spec/sinatra-admin/models/admin_spec.rb +33 -0
- data/spec/sinatra-admin/register/base_spec.rb +13 -0
- data/spec/sinatra-admin/register/custom_spec.rb +40 -0
- data/spec/sinatra-admin/register/model_spec.rb +26 -0
- data/spec/sinatra-admin/register_spec.rb +15 -0
- data/spec/sinatra-admin/version_spec.rb +5 -0
- data/spec/sinatra-admin_spec.rb +73 -0
- data/spec/spec_helper.rb +81 -0
- metadata +343 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ec4cc4370873a4a162ba234e9751b4efbaa24399
|
4
|
+
data.tar.gz: fdd0d626c74e68e08e2515358b4aad04eac33290
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a26d7a34c0de58a89125553c82e840d291bdf1887a0b4ccfd8048f9520be31966268e22c10df10adcae4a292cccd5432ea7f01d53a83d3dc7aff90c9dcb2b83c
|
7
|
+
data.tar.gz: c2586581a6eceab9b8992913d0dcd87d108936b3c3190186ea1cade3c2d36b24d527c01964e2c7b938691653e8f0363429a6eb979e6089c319169475ea7a4106
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Fco. Delgado
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
# SinatraAdmin
|
2
|
+
|
3
|
+
Sinatra application that allows us to have an admin dashboard with
|
4
|
+
minimal effort.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'sinatra-admin'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install sinatra-admin
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
By default SinatraAdmin assumes that it's mounted over an "admin"
|
25
|
+
namespace. This is how SinatraAdmin should be configured:
|
26
|
+
|
27
|
+
1. Define your project namespace and your Sinatra applications.
|
28
|
+
```ruby
|
29
|
+
module MyApp
|
30
|
+
class API < Sinatra::Base
|
31
|
+
#Config for API here
|
32
|
+
end
|
33
|
+
|
34
|
+
class Admin < Sinatra::Base
|
35
|
+
#Config for Admin here
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
2. Add SinatraAdmin::App middleware to your admin application.
|
41
|
+
```ruby
|
42
|
+
class MyApp::Admin
|
43
|
+
use SinatraAdmin::App
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
3. Register model resources(let's assume that we have User and Comment
|
48
|
+
models). It creates the 7 REST actions to /create/update/remove
|
49
|
+
records.
|
50
|
+
```ruby
|
51
|
+
class MyApp::Admin
|
52
|
+
SinatraAdmin.register 'User'
|
53
|
+
SinatraAdmin.register 'Comment'
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
4. Define your root resource(optional). This is going to be the first
|
58
|
+
page where the application is going to redirect you after the login.
|
59
|
+
SinatraAdmin defines the first registered resource as the default root.
|
60
|
+
In this case it will get 'User'(according to point number 3). If you
|
61
|
+
want, you can set a different resource as the default one.
|
62
|
+
```ruby
|
63
|
+
class MyApp::Admin
|
64
|
+
SinatraAdmin.root 'Comment'
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
5. Define your custom resources. Having model resources sometimes is not
|
69
|
+
enough and we might want to see some stats about our application. An
|
70
|
+
example could be: "As an admin I want to see how many user accounts has
|
71
|
+
been registered". Let's take a look at how to define custom resources.
|
72
|
+
```ruby
|
73
|
+
class MyApp::Admin
|
74
|
+
SinatraAdmin.register 'Stats' do
|
75
|
+
get '/?' do
|
76
|
+
@message = 'Welcome to SinatraAdmin custom pages!'
|
77
|
+
@accounts_counter = User.count
|
78
|
+
haml 'stats/index'.to_sym
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
```
|
83
|
+
If you try to access that custom page you are going to see an exception
|
84
|
+
saying something like: "Errno::ENOENT at /admin/stats No such file or
|
85
|
+
directory - /path/to/the/gem/sinatra-admin/views/stats/index.haml"
|
86
|
+
It's because SinatraAdmin tries to find the template in the views
|
87
|
+
folder of sinatra-admin. Obviously, that custom template does not exist
|
88
|
+
in the gem. SinatraAdmin has a method to extend the views path and it
|
89
|
+
allows us to find the template that we are looking for. This takes us to
|
90
|
+
the next point.
|
91
|
+
|
92
|
+
6. Extend your views path(Only for custom resources). SinatraAdmin has
|
93
|
+
the method :extend_views_from. This method receives a value that
|
94
|
+
should either be a String instance with the path to views folder or
|
95
|
+
be a Sinatra application. SinatraAdmin expects to be mounted over an
|
96
|
+
"admin" namespace, that's why it's going to look the view in:
|
97
|
+
the/extented/path/admin
|
98
|
+
```ruby
|
99
|
+
class MyApp::Admin
|
100
|
+
SinatraAdmin.extend_views_from(MyApp::API) #It'll look at path/to/my_app/api/views/admin/stats/index.haml
|
101
|
+
SinatraAdmin.extend_views_from('path/to/views/folder') #It'll look at path/to/views/folder/admin/stats/index.haml
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
7. Wrapping it up.
|
106
|
+
```ruby
|
107
|
+
class MyApp::Admin < Sinatra::Base
|
108
|
+
use SinatraAmin::App
|
109
|
+
SinatraAdmin.root 'Post'
|
110
|
+
SinatraAdmin.resource 'User'
|
111
|
+
SinatraAdmin.resource 'Comment'
|
112
|
+
SinatraAdmin.resource 'Post'
|
113
|
+
SinatraAdmin.register 'Stats' do
|
114
|
+
get '/?' do
|
115
|
+
@message = 'Welcome to SinatraAdmin custom pages!'
|
116
|
+
@accounts_counter = User.count
|
117
|
+
haml 'stats/index'.to_sym
|
118
|
+
end
|
119
|
+
end
|
120
|
+
SinatraAdmin.extend_views_from(MyApp::API)
|
121
|
+
end
|
122
|
+
```
|
123
|
+
|
124
|
+
8. Run it. We assume you're using a config.ru file to run your Sinatra
|
125
|
+
application. This is how it should look like:
|
126
|
+
```ruby
|
127
|
+
require 'path/to/my_app/api'
|
128
|
+
require 'path/to/my_app/admin'
|
129
|
+
|
130
|
+
map "/api"
|
131
|
+
run MyApp::API
|
132
|
+
end
|
133
|
+
|
134
|
+
map "/admin" do #mounted over "/admin" namespace(mandatory)
|
135
|
+
run MyApp::Admin
|
136
|
+
end
|
137
|
+
```
|
138
|
+
|
139
|
+
## Constraints
|
140
|
+
|
141
|
+
* SinatraAdmin only works if you mount it over "admin" namespace like in
|
142
|
+
the example(Point 8).
|
143
|
+
|
144
|
+
* Even when you pass a block with get/post/put/patch/delete when you
|
145
|
+
register a model resource(like User), SinatraAdmin does not have a way to access
|
146
|
+
them(only the URL). This is a TODO feature.
|
147
|
+
|
148
|
+
* Even when you pass a block with post/put/patch/delete when you
|
149
|
+
register a custom resource(Like MyStats), SinatraAdmin does not have a way to
|
150
|
+
access them(only the URL). This is a TODO feature.
|
151
|
+
|
152
|
+
## Notes
|
153
|
+
|
154
|
+
* SinatraAdmin uses Mongoid by default. TODO: Add activeRecord support.
|
155
|
+
|
156
|
+
* SinatraAdmin uses Warden for authentication.
|
157
|
+
|
158
|
+
* SinatraAdmin comes with an Admin model by default. The constant is
|
159
|
+
SinatraAdmin::Admin. It has :first_name, :last_name, :email and
|
160
|
+
:password fields. Password is encrypted and stored in :password_hash
|
161
|
+
field. :email and :password are required fields and :email should have a correct format.
|
162
|
+
|
163
|
+
* You can contribute to this Project. Contributing means not only adding
|
164
|
+
features but also writing documentation, adding issues, refactoring code
|
165
|
+
or just sending us either a <3 if you liked the project or a </3 if you
|
166
|
+
did not like it ;)
|
167
|
+
|
168
|
+
* Current version: 0.1.0
|
169
|
+
|
170
|
+
## Contributing
|
171
|
+
|
172
|
+
1. Fork it ( https://github.com/[my-github-username]/sinatra-admin/fork )
|
173
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
174
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
175
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
176
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/dummy/config.ru
ADDED
data/dummy/dummy.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'sinatra-admin'
|
2
|
+
require 'sinatra/base'
|
3
|
+
|
4
|
+
require_relative 'models/user'
|
5
|
+
require_relative 'models/comment'
|
6
|
+
|
7
|
+
module Dummy
|
8
|
+
class Base < Sinatra::Base
|
9
|
+
set :root, File.dirname(__FILE__)
|
10
|
+
Mongoid.load!("dummy/config/mongoid.yml")
|
11
|
+
I18n.enforce_available_locales = false
|
12
|
+
end
|
13
|
+
|
14
|
+
class API < Base
|
15
|
+
end
|
16
|
+
|
17
|
+
class Admin < Base
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
Feature: Admin login
|
2
|
+
In order to use SinatraAdmin
|
3
|
+
As an Admin
|
4
|
+
I want to login and use the admin application
|
5
|
+
|
6
|
+
Scenario: Admin is able to login with valid credentials
|
7
|
+
Given I add SinatraAdmin as middleware
|
8
|
+
And I am an Admin
|
9
|
+
And I register "User" resource
|
10
|
+
And I am on the login page
|
11
|
+
When I fill in "email" with "admin@mail.com"
|
12
|
+
And I fill in "password" with "admin"
|
13
|
+
And I press "Login"
|
14
|
+
Then I should see "Users - Index"
|
15
|
+
|
16
|
+
Scenario: Admin is not able to login with invalid email
|
17
|
+
Given I add SinatraAdmin as middleware
|
18
|
+
And I am an Admin
|
19
|
+
And I register "User" resource
|
20
|
+
And I am on the login page
|
21
|
+
When I fill in "email" with "invalidemail@mail.com"
|
22
|
+
And I fill in "password" with "admin"
|
23
|
+
And I press "Login"
|
24
|
+
Then I should see "Login - SinatraAdmin"
|
25
|
+
Then I should see "The email you entered does not exist"
|
26
|
+
|
27
|
+
Scenario: Admin is not able to login with invalid password
|
28
|
+
Given I add SinatraAdmin as middleware
|
29
|
+
And I am an Admin
|
30
|
+
And I register "User" resource
|
31
|
+
And I am on the login page
|
32
|
+
When I fill in "email" with "admin@mail.com"
|
33
|
+
And I fill in "password" with "invalid_password"
|
34
|
+
And I press "Login"
|
35
|
+
Then I should see "Login - SinatraAdmin"
|
36
|
+
Then I should see "You entered an incorrect password"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Feature: Admin login
|
2
|
+
In order to use SinatraAdmin
|
3
|
+
As a logged in Admin
|
4
|
+
I want to logout when I click the "Lougout" link
|
5
|
+
|
6
|
+
Scenario: Admin is able to logout
|
7
|
+
Given I add SinatraAdmin as middleware
|
8
|
+
And I am an Admin
|
9
|
+
And I register "User" resource
|
10
|
+
And I am logged in as admin
|
11
|
+
And I am on the home page
|
12
|
+
When I follow "Logout"
|
13
|
+
Then I should see "Login - SinatraAdmin"
|
14
|
+
Then I should see "Successfully logged out"
|
@@ -0,0 +1,44 @@
|
|
1
|
+
Feature: Creating Users
|
2
|
+
In order to use SinatraAdmin
|
3
|
+
As an Admin
|
4
|
+
I want to create users when I register the "User" resource
|
5
|
+
|
6
|
+
Scenario: Admin tries to create a user without login
|
7
|
+
Given I add SinatraAdmin as middleware
|
8
|
+
And I register "User" resource
|
9
|
+
And I am an Admin
|
10
|
+
When I go to users listing
|
11
|
+
Then I should see "Login - SinatraAdmin"
|
12
|
+
And I should see "You must log in"
|
13
|
+
|
14
|
+
Scenario: Admin creates user
|
15
|
+
Given I add SinatraAdmin as middleware
|
16
|
+
And I register "User" resource
|
17
|
+
And I am an Admin
|
18
|
+
And I am logged in as admin
|
19
|
+
And I am on users listing
|
20
|
+
When I follow "New"
|
21
|
+
And I fill in "first_name" with "Vahak"
|
22
|
+
And I fill in "last_name" with "Matavosian"
|
23
|
+
And I fill in "email" with "vahak@herbalife.com"
|
24
|
+
And I press "Create"
|
25
|
+
Then I should see "User - Show"
|
26
|
+
Then I should see "_id"
|
27
|
+
Then I should see "first_name"
|
28
|
+
Then I should see "last_name"
|
29
|
+
Then I should see "email"
|
30
|
+
Then I should see "Vahak"
|
31
|
+
Then I should see "Matavosian"
|
32
|
+
Then I should see "vahak@herbalife.com"
|
33
|
+
|
34
|
+
Scenario: Admin creates user with errors
|
35
|
+
Given I add SinatraAdmin as middleware
|
36
|
+
And I register "User" resource
|
37
|
+
And I am an Admin
|
38
|
+
And I am logged in as admin
|
39
|
+
And I am on users listing
|
40
|
+
When I follow "New"
|
41
|
+
And I fill in "first_name" with "Vahak"
|
42
|
+
And I fill in "last_name" with "Matavosian"
|
43
|
+
And I press "Create"
|
44
|
+
Then I should see "email ["can't be blank"]"
|
@@ -0,0 +1,34 @@
|
|
1
|
+
Feature: Custom Pages
|
2
|
+
In order to use SinatraAdmin
|
3
|
+
As an Admin
|
4
|
+
I want to add a custom page
|
5
|
+
|
6
|
+
Scenario: Admin tries to see custom page without login
|
7
|
+
Given I add SinatraAdmin as middleware
|
8
|
+
And I add main app views to SinatraAdmin views
|
9
|
+
And I register my custom page
|
10
|
+
And I am an Admin
|
11
|
+
When I go to my custom page
|
12
|
+
Then I should see "Login - SinatraAdmin"
|
13
|
+
And I should see "You must log in"
|
14
|
+
|
15
|
+
Scenario: Admin sees custom page
|
16
|
+
Given I add SinatraAdmin as middleware
|
17
|
+
And I add main app views to SinatraAdmin views
|
18
|
+
And I register my custom page
|
19
|
+
And I am an Admin
|
20
|
+
And I am logged in as admin
|
21
|
+
And There are users
|
22
|
+
When I go to my custom page
|
23
|
+
And I should see "Welcome to SinatraAdmin custom pages!"
|
24
|
+
And I should see "Admin Count: 1"
|
25
|
+
|
26
|
+
Scenario: Admin adds custom page to a Resource(model)
|
27
|
+
Given I add SinatraAdmin as middleware
|
28
|
+
And I add main app views to SinatraAdmin views
|
29
|
+
And I am an Admin
|
30
|
+
And I am logged in as admin
|
31
|
+
And There are users
|
32
|
+
And I register "User" resource with custom route
|
33
|
+
When I go to users custom page
|
34
|
+
And I should see "Welcome to Resource model custom page"
|