survey_generator 0.1.0 → 0.2.0

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.
Files changed (48) hide show
  1. checksums.yaml +15 -0
  2. data/lib/generators/survey/USAGE +10 -10
  3. data/lib/generators/survey/metamodels/survey_metamodel.rb +6 -5
  4. data/lib/generators/survey/survey_generator.rb +15 -15
  5. data/lib/generators/survey/templates/app/app.tpl +4 -6
  6. data/lib/generators/survey/templates/app/assets/assets.tpl +4 -0
  7. data/lib/generators/survey/templates/app/assets/stylesheets/stylesheet.tpl +188 -183
  8. data/lib/generators/survey/templates/app/controllers/application_controller.tpl +15 -0
  9. data/lib/generators/survey/templates/app/controllers/controllers.tpl +7 -0
  10. data/lib/generators/survey/templates/app/controllers/sessions_controller.tpl +26 -0
  11. data/lib/generators/survey/templates/app/controllers/static_pages_controller.tpl +20 -0
  12. data/lib/generators/survey/templates/app/controllers/survey_controller.tpl +0 -19
  13. data/lib/generators/survey/templates/app/controllers/users_controller.tpl +82 -0
  14. data/lib/generators/survey/templates/app/helpers/helpers.tpl +5 -0
  15. data/lib/generators/survey/templates/app/helpers/sessions_helper.tpl +46 -0
  16. data/lib/generators/survey/templates/app/helpers/users_helper.tpl +14 -0
  17. data/lib/generators/survey/templates/app/models/models.tpl +6 -0
  18. data/lib/generators/survey/templates/app/models/question.tpl +10 -0
  19. data/lib/generators/survey/templates/app/models/survey.tpl +10 -0
  20. data/lib/generators/survey/templates/app/models/test.tpl +15 -0
  21. data/lib/generators/survey/templates/app/models/user.tpl +28 -0
  22. data/lib/generators/survey/templates/app/views/layouts/footer.tpl +2 -2
  23. data/lib/generators/survey/templates/app/views/layouts/header.tpl +21 -13
  24. data/lib/generators/survey/templates/app/views/sessions/new.tpl +28 -0
  25. data/lib/generators/survey/templates/app/views/shared/error_messages.tpl +1 -1
  26. data/lib/generators/survey/templates/app/views/static_pages/home.tpl +1 -1
  27. data/lib/generators/survey/templates/app/views/survey/form.tpl +40 -0
  28. data/lib/generators/survey/templates/app/views/survey/index.tpl +40 -0
  29. data/lib/generators/survey/templates/app/views/survey/table.tpl +20 -0
  30. data/lib/generators/survey/templates/app/views/survey/textfield.tpl +21 -0
  31. data/lib/generators/survey/templates/app/views/users/edit.tpl +19 -0
  32. data/lib/generators/survey/templates/app/views/users/fields.tpl +24 -0
  33. data/lib/generators/survey/templates/app/views/users/index.tpl +17 -0
  34. data/lib/generators/survey/templates/app/views/users/new.tpl +15 -0
  35. data/lib/generators/survey/templates/app/views/users/show.tpl +15 -0
  36. data/lib/generators/survey/templates/app/views/users/user.tpl +11 -0
  37. data/lib/generators/survey/templates/app/views/views.tpl +11 -2
  38. data/lib/generators/survey/templates/config/routes.tpl +7 -0
  39. data/lib/generators/survey/templates/db/db.tpl +0 -1
  40. data/lib/generators/survey/templates/db/migrate/create_questions.tpl +5 -2
  41. data/lib/generators/survey/templates/db/migrate/create_surveys.tpl +4 -1
  42. data/lib/generators/survey/templates/db/migrate/create_users.tpl +1 -1
  43. data/lib/tasks/sample_data.rake +20 -0
  44. metadata +31 -11
  45. data/lib/generators/survey/templates/app/models/survey_model.tpl +0 -45
  46. data/lib/generators/survey/templates/app/views/survey/survey_form_view.tpl +0 -37
  47. data/lib/generators/survey/templates/app/views/survey/survey_index_view.tpl +0 -38
  48. data/lib/generators/survey/templates/db/migrate/create_pages.tpl +0 -18
@@ -0,0 +1,15 @@
1
+ <% define 'application_controller', for: Survey do %>
2
+ <% file "app/controllers/application_controller.rb" do %>
3
+ class ApplicationController < ActionController::Base<%iinc%>
4
+ protect_from_forgery
5
+ include SessionsHelper
6
+
7
+ <%nl%>
8
+ # Force signout to prevent CSRF attacks
9
+ def handle_unverified request<%iinc%>
10
+ sign_out
11
+ super
12
+ <%idec%>end
13
+ <%idec%>end
14
+ <% end %>
15
+ <% end %>
@@ -0,0 +1,7 @@
1
+ <% define 'root' do %>
2
+ <% expand 'survey_controller::survey_controller' %>
3
+ <% expand 'static_pages_controller::static_pages_controller' %>
4
+ <% expand 'application_controller::application_controller' %>
5
+ <% expand 'sessions_controller::sessions_controller' %>
6
+ <% expand 'users_controller::users_controller' %>
7
+ <% end %>
@@ -0,0 +1,26 @@
1
+ <% define 'sessions_controller', for: Survey do %>
2
+ <% file "app/controllers/sessions_controller.rb" do %>
3
+ class SessionsController < ApplicationController<%iinc%>
4
+ def new
5
+ end
6
+
7
+ <%nl%>
8
+ def create<%iinc%>
9
+ user = User.find_by_email(params[:email].downcase)
10
+ if user && user.authenticate(params[:password])<%iinc%>
11
+ sign_in user
12
+ redirect_to user
13
+ <%idec%>else<%iinc%>
14
+ flash.now[:error] = 'Invalid email/password combination'
15
+ render 'new'
16
+ <%idec%>end
17
+ <%idec%>end
18
+
19
+ <%nl%>
20
+ def destroy<%iinc%>
21
+ sign_out
22
+ redirect_to root_url
23
+ <%idec%>end
24
+ <%idec%>end
25
+ <% end %>
26
+ <% end %>
@@ -0,0 +1,20 @@
1
+ <% define 'static_pages_controller' do %>
2
+ <% file "app/controllers/static_pages_controller.rb" do %>
3
+ class StaticPagesController < ApplicationController<%iinc%>
4
+ def home<%iinc%>
5
+ <%idec%>end
6
+
7
+ <%nl%>
8
+ def help<%iinc%>
9
+ <%idec%>end
10
+
11
+ <%nl%>
12
+ def about<%iinc%>
13
+ <%idec%>end
14
+
15
+ <%nl%>
16
+ def contact<%iinc%>
17
+ <%idec%>end
18
+ <%idec%>end
19
+ <% end %>
20
+ <% end %>
@@ -5,23 +5,4 @@
5
5
  end
6
6
  <%idec%>end
7
7
  <% end %>
8
-
9
- <% file "app/controllers/static_pages_controller.rb" do %>
10
- class StaticPagesController < ApplicationController<%iinc%>
11
- def home<%iinc%>
12
- <%idec%>end
13
-
14
- <%nl%>
15
- def help<%iinc%>
16
- <%idec%>end
17
-
18
- <%nl%>
19
- def about<%iinc%>
20
- <%idec%>end
21
-
22
- <%nl%>
23
- def contact<%iinc%>
24
- <%idec%>end
25
- <%idec%>end
26
- <% end %>
27
8
  <% end %>
@@ -0,0 +1,82 @@
1
+ <% define 'users_controller', for: Survey do %>
2
+ <% file "app/controllers/users_controller.rb" do %>
3
+ class UsersController < ApplicationController<%iinc%>
4
+ before_filter :signed_in_user, only: [:index, :edit, :update, :destroy]
5
+ before_filter :correct_user, only: [:edit, :update]
6
+ before_filter :admin_user, only: :destroy
7
+
8
+ <%nl%>
9
+ def index<%iinc%>
10
+ @users = User.paginate(page: params[:page])
11
+ <%idec%>end
12
+
13
+ <%nl%>
14
+ def show<%iinc%>
15
+ @user = User.find(params[:id])
16
+ <%idec%>end
17
+
18
+ <%nl%>
19
+ def new<%iinc%>
20
+ if signed_in?<%iinc%>
21
+ redirect_to root_url
22
+ <%idec%>else<%iinc%>
23
+ @user = User.new
24
+ <%idec%>end
25
+ <%idec%>end
26
+
27
+ <%nl%>
28
+ def create<%iinc%>
29
+ if signed_in?<%iinc%>
30
+ redirect_to root_url
31
+ <%idec%>else<%iinc%>
32
+ @user = User.new(params[:user])
33
+ if @user.save<%iinc%>
34
+ sign_in @user
35
+ flash[:success] = "Welcome to the Sample App!"
36
+ redirect_to @user
37
+ <%idec%>else<%iinc%>
38
+ render 'new'
39
+ <%idec%>end
40
+ <%idec%>end
41
+ <%idec%>end
42
+
43
+ <%nl%>
44
+ def edit
45
+ end
46
+
47
+ <%nl%>
48
+ def update<%iinc%>
49
+ if @user.update_attributes(params[:user])<%iinc%>
50
+ flash[:success] = "Profile updated"
51
+ sign_in @user
52
+ redirect_to @user
53
+ <%idec%>else<%iinc%>
54
+ render 'edit'
55
+ <%idec%>end
56
+ <%idec%>end
57
+
58
+ <%nl%>
59
+ def destroy<%iinc%>
60
+ if current_user.id != params[:id]<%iinc%>
61
+ User.find(params[:id]).destroy
62
+ flash[:success] = "User destroyed."
63
+ <%idec%>end
64
+ redirect_to users_url
65
+ <%idec%>end
66
+
67
+ <%nl%>
68
+ private<%iinc%>
69
+
70
+ <%nl%>
71
+ def correct_user<%iinc%>
72
+ @user = User.find(params[:id])
73
+ redirect_to(root_path) unless current_user?(@user)
74
+ <%idec%>end
75
+
76
+ <%nl%>
77
+ def admin_user<%iinc%>
78
+ redirect_to(root_path) unless current_user.admin?
79
+ <%idec%>end
80
+ <%idec%><%idec%>end
81
+ <% end %>
82
+ <% end %>
@@ -0,0 +1,5 @@
1
+ <% define 'root' do %>
2
+ <% expand 'application_helper::application_helper' %>
3
+ <% expand 'sessions_helper::sessions_helper' %>
4
+ <% expand 'users_helper::users_helper' %>
5
+ <% end %>
@@ -0,0 +1,46 @@
1
+ <% define 'sessions_helper' do %>
2
+ <% file "app/helpers/sessions_helper.rb" do %>
3
+ module SessionsHelper<%iinc%>
4
+
5
+ <%nl%>
6
+ def sign_in(user)<%iinc%>
7
+ cookies.permanent[:remember_token] = user.remember_token
8
+ self.current_user = user
9
+ <%idec%>end
10
+
11
+ <%nl%>
12
+ def signed_in?<%iinc%>
13
+ !current_user.nil?
14
+ <%idec%>end
15
+
16
+ <%nl%>
17
+ def sign_out<%iinc%>
18
+ self.current_user = nil
19
+ cookies.delete(:remember_token)
20
+ <%idec%>end
21
+
22
+ <%nl%>
23
+ def current_user=(user)<%iinc%>
24
+ @current_user = user
25
+ <%idec%>end
26
+
27
+ <%nl%>
28
+ def current_user<%iinc%>
29
+ @current_user ||= User.find_by_remember_token(cookies[:remember_token])
30
+ <%idec%>end
31
+
32
+ <%nl%>
33
+ def current_user?(user)<%iinc%>
34
+ user == current_user
35
+ <%idec%>end
36
+
37
+ <%nl%>
38
+ def signed_in_user<%iinc%>
39
+ unless signed_in?<%iinc%>
40
+ store_location
41
+ redirect_to signin_url, notice: "Please sign in."
42
+ <%idec%>end
43
+ <%idec%>end
44
+ <%idec%>end
45
+ <% end %>
46
+ <% end %>
@@ -0,0 +1,14 @@
1
+ <% define 'users_helper' do %>
2
+ <% file "app/helpers/users_helper.rb" do %>
3
+ module UsersHelper<%iinc%>
4
+
5
+ <%nl%>
6
+ #Returns the Gravatar (http:/gravatar.com/) for the given user.
7
+ def gravatar_for(user, size)<%iinc%>
8
+ gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
9
+ gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
10
+ image_tag(gravatar_url, alt: user.name, class: "gravatar")
11
+ <%idec%>end
12
+ <%idec%>end
13
+ <% end %>
14
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <% define 'root' do %>
2
+ <% expand 'question::question' %>
3
+ <% expand 'survey::survey' %>
4
+ <% expand 'test::test' %>
5
+ <% expand 'user::user' %>
6
+ <% end %>
@@ -0,0 +1,10 @@
1
+ <% define 'question' do %>
2
+ <% file "app/models/question.rb" do %>
3
+ class Question < ActiveRecord::Base<%iinc%>
4
+ attr_accessible :content, :name, :survey_id
5
+
6
+ <%nl%>
7
+ belongs_to :survey
8
+ <%idec%>end
9
+ <% end %>
10
+ <% end %>
@@ -0,0 +1,10 @@
1
+ <% define 'survey' do %>
2
+ <% file "app/models/survey.rb" do %>
3
+ class Survey < ActiveRecord::Base<%iinc%>
4
+ attr_accessible :author, :name, :title, :user_id
5
+
6
+ <%nl%>
7
+ has_many :questions, dependent: :destroy
8
+ <%idec%>end
9
+ <% end %>
10
+ <% end %>
@@ -0,0 +1,15 @@
1
+ <% define 'test', for: Survey do %>
2
+ <% file "app/models/" + name.downcase + ".rb" do %>
3
+ class <%= name %> < ActiveRecord::Base<%iinc%>
4
+ <% questions.each do |question| %>
5
+ <% if !question.is_a?(PageBreak) %>
6
+ <% expand 'question', for: question %>
7
+ <% end %>
8
+ <% end %>
9
+ <%idec%>end
10
+ <% end %>
11
+ <% end %>
12
+
13
+ <% define 'question', for: Question do %>
14
+ attr_accessible :<%= name %>
15
+ <% end %>
@@ -0,0 +1,28 @@
1
+ <% define 'user' do %>
2
+ <% file "app/models/user.rb" do %>
3
+ class User < ActiveRecord::Base<%iinc%>
4
+ attr_accessible :name, :email, :password, :password_confirmation
5
+ has_secure_password
6
+ has_many :surveys, dependent: :destroy
7
+
8
+ <%nl%>
9
+ before_save { email.downcase! }
10
+ before_save :create_remember_token
11
+
12
+ <%nl%>
13
+ VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
14
+ validates :name, presence: true, length: { maximum: 50 }
15
+ validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
16
+ validates :password, presence: true, length: { minimum: 6 }
17
+ validates :password_confirmation, presence: true
18
+
19
+ <%nl%>
20
+ private<%iinc%>
21
+
22
+ <%nl%>
23
+ def create_remember_token<%iinc%>
24
+ self.remember_token = SecureRandom.urlsafe_base64
25
+ <%idec%>end
26
+ <%idec%><%idec%>end
27
+ <% end %>
28
+ <% end %>
@@ -2,8 +2,8 @@
2
2
  <% file "app/views/layouts/_footer.html.erb" do %>
3
3
  <footer class="footer">
4
4
  <small><%iinc%>
5
- <a href="http://railstutorial.org/">Rails Tutorial</a>
6
- by Michael Hartl
5
+ <a href="#">Survey generator</a>
6
+ by Daniel Kemper
7
7
  <%idec%></small>
8
8
  <nav><%iinc%>
9
9
  <ul><%iinc%>
@@ -8,7 +8,11 @@
8
8
  <ul class="nav pull-right"><%iinc%>
9
9
  <li><%%= link_to "Home", root_path %></li>
10
10
  <li><%%= link_to "Help", help_path %></li>
11
- <!--<li><%%= link_to "Users", '#' %></li>-->
11
+
12
+ <%nl%>
13
+ <%% if signed_in? %><%iinc%>
14
+ <li><%%= link_to "Users", users_path %></li>
15
+ <%% end %>
12
16
 
13
17
  <%nl%>
14
18
  <li id="fat-menu" class="dropdown"><%iinc%>
@@ -16,22 +20,26 @@
16
20
  Surveys <b class="caret"></b>
17
21
  <%idec%></a>
18
22
  <ul class="dropdown-menu"><%iinc%>
19
- <li><%%= link_to "<%= name.pluralize.capitalize %>", <%= name.pluralize.downcase %>_path %></li>
23
+ <li><%%= link_to "<%= title.capitalize %>", <%= name.pluralize.downcase %>_path %></li>
20
24
  <%idec%></ul>
21
25
  <%idec%></li>
22
26
 
23
27
  <%nl%>
24
- <!--<li id="fat-menu" class="dropdown"><%iinc%>
25
- <a href="#" class="dropdown-toggle" data-toggle="dropdown"><%iinc%>
26
- Account <b class="caret"></b>
27
- <%idec%></a>
28
- <ul class="dropdown-menu"><%iinc%>
29
- <li><%%= link_to "Profile", '#' %></li>
30
- <li><%%= link_to "Settings", '#' %></li>
31
- <li class="divider"></li>
32
- <li><%%= link_to "Sign out", '#' %></li>
33
- <%idec%></ul>
34
- <%idec%></li>-->
28
+ <%% if signed_in? %><%iinc%>
29
+ <li id="fat-menu" class="dropdown"><%iinc%>
30
+ <a href="#" class="dropdown-toggle" data-toggle="dropdown"><%iinc%>
31
+ Account <b class="caret"></b>
32
+ <%idec%></a>
33
+ <ul class="dropdown-menu"><%iinc%>
34
+ <li><%%= link_to "Profile", current_user %></li>
35
+ <li><%%= link_to "Settings", edit_user_path(current_user) %></li>
36
+ <li class="divider"></li>
37
+ <li><%%= link_to "Sign out", signout_path, method: "delete" %></li>
38
+ <%idec%></ul>
39
+ <%idec%></li>
40
+ <%idec%><%% else %><%iinc%>
41
+ <li><%%= link_to "Sign in", signin_path %></li>
42
+ <%idec%><%% end %>
35
43
  <%idec%></ul>
36
44
  <%idec%></nav>
37
45
  <%idec%></div>
@@ -0,0 +1,28 @@
1
+ <% define 'new', for: Survey do %>
2
+ <% file "app/views/sessions/new.html.erb" do %>
3
+ <%% provide(:title, "Sign in") %>
4
+ <h1>Sign in</h1>
5
+
6
+ <%nl%>
7
+ <div class="row"><%iinc%>
8
+ <div class="span6 offset3"><%iinc%>
9
+ <%%= form_tag sessions_path do %><%iinc%>
10
+ <div class="field"><%iinc%>
11
+ <%%= label_tag :email %>
12
+ <%%= text_field_tag :email, params[:email] %>
13
+ <%idec%></div>
14
+ <div class="field"><%iinc%>
15
+ <%%= label_tag :password %>
16
+ <%%= password_field_tag :password %>
17
+ <%idec%></div>
18
+
19
+ <%nl%>
20
+ <div class="actions"><%%= submit_tag "Sign in", class: "btn btn-large btn-primary" %></div>
21
+ <%idec%><%% end %>
22
+
23
+ <%nl%>
24
+ <p>New user? <%%= link_to "Sign up now!", signup_path %></p>
25
+ <%idec%></div>
26
+ <%idec%></div>
27
+ <% end %>
28
+ <% end %>
@@ -1,4 +1,4 @@
1
- <% define 'error_messages', for: Survey do %>
1
+ <% define 'error_messages' do %>
2
2
  <% file "app/views/shared/_error_messages.html.erb" do %>
3
3
  <%% if object.errors.any? %><%iinc%>
4
4
  <div id="error_explanation"><%iinc%>
@@ -9,7 +9,7 @@
9
9
  <%idec%></h2>
10
10
 
11
11
  <%nl%>
12
- <%%= link_to "Sign up now!", '#', class: "btn btn-large btn-primary" %>
12
+ <%%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
13
13
  <%idec%></div>
14
14
 
15
15
  <%nl%>