tb_core 1.3.2 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +101 -51
  3. data/app/assets/javascripts/admin/core/application.js +1 -5
  4. data/app/assets/javascripts/tb_core/remote.js +179 -0
  5. data/app/assets/libs/jquery-ui/jquery-ui.scss +16 -16
  6. data/app/assets/stylesheets/admin/core/{application.css.scss → application.scss} +22 -0
  7. data/app/assets/stylesheets/admin/core/{login.css.scss → login.scss} +0 -0
  8. data/app/controllers/admin/application_controller.rb +6 -0
  9. data/app/controllers/admin/users_controller.rb +4 -10
  10. data/app/controllers/spud/application_controller.rb +8 -2
  11. data/app/controllers/user_sessions_controller.rb +1 -1
  12. data/app/helpers/tb_core/application_helper.rb +88 -0
  13. data/app/views/admin/users/_form.html.erb +21 -17
  14. data/app/views/admin/users/edit.html.erb +1 -3
  15. data/app/views/admin/users/new.html.erb +1 -3
  16. data/app/views/layouts/admin/_search.html.erb +3 -0
  17. data/app/views/layouts/admin/detail.html.erb +1 -1
  18. data/app/views/layouts/admin/error_page.html.erb +10 -12
  19. data/config/locales/en.yml +8 -0
  20. data/lib/generators/spud/controller_spec_generator.rb +34 -0
  21. data/lib/generators/spud/module_generator.rb +37 -0
  22. data/lib/generators/spud/setup_generator.rb +5 -0
  23. data/lib/generators/spud/templates/admin_controller.rb.erb +2 -7
  24. data/lib/generators/spud/templates/assets/application.scss +1 -0
  25. data/lib/generators/spud/templates/controller.rb.erb +1 -5
  26. data/lib/generators/spud/templates/controller_spec.rb.erb +79 -0
  27. data/lib/generators/spud/templates/views/admin/_form.html.erb +2 -2
  28. data/lib/generators/spud/templates/views/layouts/application.html.erb +2 -3
  29. data/lib/spud_core/belongs_to_app.rb +1 -1
  30. data/lib/spud_core/engine.rb +2 -0
  31. data/lib/spud_core/errors.rb +23 -14
  32. data/lib/spud_core/version.rb +1 -1
  33. data/lib/tb_core/form_builder.rb +11 -1
  34. data/lib/tb_core/responder.rb +32 -0
  35. data/lib/tb_core/test_helper.rb +42 -0
  36. data/spec/controllers/admin/dashboard_controller_spec.rb +3 -7
  37. data/spec/controllers/admin/settings_controller_spec.rb +1 -4
  38. data/spec/controllers/admin/user_sessions_controller_spec.rb +1 -2
  39. data/spec/controllers/admin/users_controller_spec.rb +1 -4
  40. data/spec/dummy/app/assets/stylesheets/admin/{application.css.scss → application.scss} +0 -0
  41. data/spec/dummy/app/assets/stylesheets/{application.css.scss → application.scss} +0 -0
  42. data/spec/rails_helper.rb +2 -1
  43. metadata +14 -14
  44. data/app/helpers/spud/application_helper.rb +0 -36
  45. data/app/helpers/twice_baked/application_helper.rb +0 -42
  46. data/spec/authlogic_helper.rb +0 -2
  47. data/spec/helpers/spud/application_helper_spec.rb +0 -11
@@ -0,0 +1,88 @@
1
+ module TbCore::ApplicationHelper
2
+
3
+ def tb_form_for(record, options = {}, &block)
4
+ options[:builder] = TbCore::FormBuilder
5
+
6
+ options[:html] ||= {}
7
+ if options[:html][:class]
8
+ options[:html][:class] += ' form-horizontal'
9
+ else
10
+ options[:html][:class] = 'form-horizontal'
11
+ end
12
+
13
+ return form_for(record, options, &block)
14
+ end
15
+
16
+ def tb_form_errors(record, *fields_to_display)
17
+ if record.errors.any?
18
+ content_tag :div, :class => 'form-errors' do
19
+ concat(tb_form_error_header(record))
20
+ concat(raw "<ul>")
21
+ if fields_to_display.any?
22
+ messages = fields_to_display.collect{ |field| record.errors.full_messages_for(field) }.flatten()
23
+ else
24
+ messages = record.errors.full_messages
25
+ end
26
+ concat(raw messages.collect{ |msg| "<li>#{msg}</li>" }.join())
27
+ concat(raw "</ul>")
28
+ end
29
+ end
30
+ end
31
+
32
+ def tb_form_error_header(record)
33
+ if record.errors.any?
34
+ message = "Please correct the following #{'error'.pluralize(record.errors.size)}:"
35
+ return content_tag :h4, message, :class => 'form-field-error'
36
+ end
37
+ end
38
+
39
+ def tb_form_error_field(record, attribute)
40
+ message = record.errors[attribute].first
41
+ if message
42
+ return content_tag :p, message, :class => 'help-block form-field-error'
43
+ end
44
+ end
45
+
46
+ def tb_form_field(attribute)
47
+ content_tag :div, :class => 'form-group' do
48
+ concat label_tag attribute, attribute, :class => 'col-sm-2 control-label'
49
+ concat(content_tag(:div, :class => 'col-sm-10') do
50
+ text_field_tag attribute, :placeholder => attribute, :class => 'form-control'
51
+ end)
52
+ end
53
+ end
54
+
55
+ def tb_page_title
56
+ if content_for?(:title)
57
+ title = content_for(:title) + ' : ' + Spud::Core.site_name
58
+ elsif @page_title
59
+ title = @page_title + ' : ' + Spud::Core.site_name
60
+ else
61
+ title = Spud::Core.site_name
62
+ end
63
+ return content_tag :title, title
64
+ end
65
+
66
+ def current_site_name
67
+ return Spud::Core.config.site_name
68
+ end
69
+
70
+ def cache_key_for_spud_collection(collection, key:'view', cache_params:[], for_user:false)
71
+ cache_keys = [controller_name, action_name, key]
72
+ cache_keys << collection.collect(&:updated_at).max().try(:utc).try(:to_i)
73
+ if for_user
74
+ cache_keys << current_user_id
75
+ end
76
+ if cache_params.any?
77
+ cache_keys += cache_params.collect{ |cache_param| params[cache_param] || 'nil' }
78
+ end
79
+ cache_keys += collection.collect(&:id)
80
+ cache_key = cache_keys.join('/')
81
+ if cache_key.length > 250
82
+ return Digest::SHA1.hexdigest(cache_key)
83
+ else
84
+ return cache_key
85
+ end
86
+ end
87
+
88
+ end
@@ -1,25 +1,29 @@
1
- <%= tb_form_error_header(f.object) %>
1
+ <%= tb_form_for @user, :url => path, :remote => true, :data => {:errors => :inline, :success => admin_users_path} do |f| %>
2
+
3
+ <%= tb_form_error_header(f.object) %>
2
4
 
3
- <h4>User Details</h4>
5
+ <h4>User Details</h4>
4
6
 
5
- <% if !Spud::Core.config.use_email_as_login %>
6
- <%= f.tb_text_field :login %>
7
- <% end %>
8
- <%= f.tb_text_field :email %>
9
- <%= f.tb_text_field :first_name %>
10
- <%= f.tb_text_field :last_name %>
11
- <%= f.tb_time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones, {:default => Rails.application.config.time_zone} %>
7
+ <% if !Spud::Core.config.use_email_as_login %>
8
+ <%= f.tb_text_field :login %>
9
+ <% end %>
10
+ <%= f.tb_text_field :email %>
11
+ <%= f.tb_text_field :first_name %>
12
+ <%= f.tb_text_field :last_name %>
13
+ <%= f.tb_time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones, {:default => Rails.application.config.time_zone} %>
14
+
15
+ <%= render :partial => 'form_additions', :locals => {:f => f} %>
12
16
 
13
- <%= render :partial => 'form_additions', :locals => {:f => f} %>
17
+ <h4>Credentials</h4>
14
18
 
15
- <h4>Credentials</h4>
19
+ <%= f.tb_password_field :password %>
20
+ <%= f.tb_password_field :password_confirmation %>
16
21
 
17
- <%= f.tb_password_field :password %>
18
- <%= f.tb_password_field :password_confirmation %>
22
+ <h4>Permissions</h4>
19
23
 
20
- <h4>Permissions</h4>
24
+ <%= f.tb_check_box :super_admin, :help_block => 'The super administrator bypasses all permissions and roles.' %>
25
+ <%= f.tb_select :spud_role_id, options_from_collection_for_select(SpudRole.all, :id, :name, f.object.spud_role_id), {:include_blank => 'No Role'} %>
21
26
 
22
- <%= f.tb_check_box :super_admin, :help_block => 'The super administrator bypasses all permissions and roles.' %>
23
- <%= f.tb_select :spud_role_id, options_from_collection_for_select(SpudRole.all, :id, :name, f.object.spud_role_id), {:include_blank => 'No Role'} %>
27
+ <%= f.tb_save_buttons('User', @user.new_record? ? admin_users_path : admin_user_path(@user)) %>
24
28
 
25
- <%= f.tb_save_buttons('User', @user.new_record? ? admin_users_path : admin_user_path(@user)) %>
29
+ <% end %>
@@ -1,3 +1 @@
1
- <%= form_for @user, :url => admin_user_path(@user), :builder => TbCore::FormBuilder, :html => {:class => "form-horizontal"} do |f| %>
2
- <%= render :partial => "form", :locals => {:f => f} %>
3
- <% end %>
1
+ <%= render :partial => "form", :locals => {:path => admin_user_path(@user)} %>
@@ -1,3 +1 @@
1
- <%= form_for @user, :url => admin_users_path, :builder => TbCore::FormBuilder, :html=> {:class => "form-horizontal admin-user-form"} do |f| %>
2
- <%= render :partial => "form", :locals => {:f => f} %>
3
- <% end %>
1
+ <%= render :partial => "form", :locals => {:path => admin_users_path} %>
@@ -8,6 +8,9 @@
8
8
  <%= link_to raw('<span class="glyphicon glyphicon-remove"></span>'), search_path %>
9
9
  </div>
10
10
  <% end %>
11
+ <% if params[:tab] %>
12
+ <%= hidden_field_tag :tab, params[:tab] %>
13
+ <% end %>
11
14
  </div>
12
15
  </div>
13
16
  <% end %>
@@ -6,7 +6,7 @@
6
6
  <h2 class="detail-title">
7
7
  <% if @page_thumbnail %>
8
8
  <%= image_tag(@page_thumbnail, :class => 'app-thumbnail') %>
9
- <% end%>
9
+ <% end %>
10
10
  <%= @page_name %>
11
11
  </h2>
12
12
  <div class="details">
@@ -1,13 +1,11 @@
1
- <%= content_for :content do %>
2
- <div class="detail-wrapper">
3
- <h2>
4
- <%= @error.code %>: <%= @error.title %>!
5
- </h2>
6
- <div id="details">
7
- <p><%= @error.message %></p>
8
- <p>Your user might not have admin rights, or you might be trying to access a module that you have not been granted permission to.</p>
9
- <p>Requested URL: <%= @error.request_url %></p>
10
- </div>
11
- </div>
1
+ <% @page_name = "#{@error.code}: #{@error.title}!" %>
2
+
3
+ <%= content_for :detail do %>
4
+ <p><%= @error.message %></p>
5
+ <% if @error.code == 403 %>
6
+ <p>Your user might not have admin rights, or you might be trying to access a module that you have not been granted permission to.</p>
7
+ <% end %>
8
+ <p>Requested URL: <%= @error.request_url %></p>
12
9
  <% end %>
13
- <%= render :template => 'layouts/admin/application' %>
10
+
11
+ <%= render :template => 'layouts/admin/detail' %>
@@ -3,6 +3,14 @@
3
3
 
4
4
  en:
5
5
  hello: "Hello world"
6
+ tb_core:
7
+ errors:
8
+ not_found:
9
+ title: "Not Found"
10
+ message: "The %{item} you were looking for could not be found."
11
+ access_denied:
12
+ title: "Access Denied"
13
+ message: "You are not authorized to view the requested %{item}."
6
14
  activerecord:
7
15
  attributes:
8
16
  spud_user:
@@ -0,0 +1,34 @@
1
+ class Spud::ControllerSpecGenerator < ::Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ argument :name, :type => :string
5
+ argument :string_attribute, :type => :string
6
+ argument :editable, :type => :numeric, :default => 0
7
+
8
+ def controller
9
+ template 'controller_spec.rb.erb', "spec/controllers/#{name.downcase}_controller_spec.rb"
10
+ end
11
+
12
+ private
13
+
14
+ def controller_class_name
15
+ return name.camelize + 'Controller'
16
+ end
17
+
18
+ def model_variable_name
19
+ return name.split('/').last.downcase.singularize
20
+ end
21
+
22
+ def model_class_name
23
+ return name.split('/').last.camelize.singularize
24
+ end
25
+
26
+ def model_string_attribute_name
27
+ return string_attribute.downcase
28
+ end
29
+
30
+ def include_edit_actions?
31
+ return editable == 1
32
+ end
33
+
34
+ end
@@ -26,6 +26,13 @@ class Spud::ModuleGenerator < ::Rails::Generators::Base
26
26
  Rails::Generators.invoke "model", [module_name_formatted.singularize] + attributes, :behavior => behavior
27
27
  Rails::Generators.invoke 'helper', [module_name], :behavior => behavior
28
28
  Rails::Generators.invoke 'helper', ["Admin/#{module_name}"], :behavior => behavior
29
+
30
+ if defined?(RSpec)
31
+ string_attribute = find_first_string_attr()
32
+ Rails::Generators.invoke 'spud:controller_spec', ["#{module_name}", string_attribute, 0], :behavior => behavior
33
+ Rails::Generators.invoke 'spud:controller_spec', ["Admin/#{module_name}", string_attribute, 1], :behavior => behavior
34
+ end
35
+
29
36
  if self.behavior == :invoke
30
37
  create_model_scopes()
31
38
  end
@@ -79,4 +86,34 @@ RUBY
79
86
  end
80
87
  end
81
88
 
89
+ # NOT CURRENTLY USED
90
+ #
91
+ # Call this method from the create_module generator to automatically build length validators for string columns. The idea here is to protect against
92
+ # database exceptions when long strings are entered by a user.
93
+ #
94
+ # TODO: This doesn't take in to account any custom :limit values on the string column. Assumes 255 as the default limit.
95
+ #
96
+ #
97
+ def add_string_maximums
98
+ string_attrs = attributes.select{ |att| att.split(':')[1] == 'string' }.collect{ |att| att.split(':')[0] }
99
+ if string_attrs.any?
100
+ string_attrs_as_symbols = string_attrs.collect{ |att| ":#{att.to_s}" }.join(', ')
101
+ inject_into_file "app/models/#{module_name.singularize.underscore}.rb", :before => 'end' do <<-RUBY
102
+
103
+ validates #{string_attrs_as_symbols}, :length => {:maximum => 255}
104
+
105
+ RUBY
106
+ end
107
+ end
108
+ end
109
+
110
+ def find_first_string_attr
111
+ found = attributes.find{ |att| att.split(':')[1] == 'string' }
112
+ if found
113
+ return found.split(':')[0]
114
+ else
115
+ return 'string_not_found'
116
+ end
117
+ end
118
+
82
119
  end
@@ -18,6 +18,11 @@ class Spud::SetupGenerator < ::Rails::Generators::Base
18
18
  template 'assets/application.js', 'app/assets/javascripts/application.js'
19
19
  template 'assets/admin/application.js', 'app/assets/javascripts/admin/application.js'
20
20
 
21
+ if defined?(Bootstrap)
22
+ theme_file = Bootstrap::Rails::Engine.root.join('templates', 'project', '_bootstrap-variables.sass')
23
+ template theme_file, 'app/assets/stylesheets/imports/bootstrap_theme.scss' if File.exist?(theme_file)
24
+ end
25
+
21
26
  environment(spud_core_configs())
22
27
  rake('railties:install:migrations')
23
28
 
@@ -1,8 +1,8 @@
1
1
  class Admin::<%= module_name_formatted.camelize%>Controller < Admin::ApplicationController
2
2
 
3
- before_action :load_<%= module_name_formatted.singularize %>, :only => [:show, :edit, :update, :destroy]
4
3
  belongs_to_spud_app :<%= module_name_formatted %>
5
4
  add_breadcrumb "<%= module_name_formatted.humanize.titlecase %>", :admin_<%= module_name_formatted %>_path
5
+ before_action :load_<%= module_name_formatted.singularize %>, :only => [:show, :edit, :update, :destroy]
6
6
 
7
7
  def index
8
8
  @<%= module_name_formatted %> = <%= module_name_formatted.singularize.camelize %>.ordered.paginate(:page => params[:page])
@@ -46,12 +46,7 @@ class Admin::<%= module_name_formatted.camelize%>Controller < Admin::Application
46
46
  private
47
47
 
48
48
  def load_<%= module_name_formatted.singularize %>
49
- @<%= module_name_formatted.singularize %> = <%= module_name_formatted.singularize.camelize %>.find_by(:id => params[:id])
50
- if @<%= module_name_formatted.singularize %>.blank?
51
- flash[:error] = "Could not find the requested <%= module_name_formatted.singularize.camelize %>"
52
- redirect_to admin_<%= module_name_formatted %>_path
53
- return false
54
- end
49
+ @<%= module_name_formatted.singularize %> = <%= module_name_formatted.singularize.camelize %>.find_by!(:id => params[:id])
55
50
  end
56
51
 
57
52
  def <%= module_name_formatted.singularize %>_params
@@ -1,5 +1,6 @@
1
1
  //= require_self
2
2
  //= require_directory .
3
3
 
4
+ @import "imports/bootstrap_theme";
4
5
  @import "bootstrap-sprockets";
5
6
  @import "bootstrap";
@@ -15,11 +15,7 @@ class <%=module_name_formatted.camelize%>Controller < ApplicationController
15
15
  private
16
16
 
17
17
  def load_<%=module_name_formatted.singularize%>
18
- @<%=module_name_formatted.singularize%> = <%=module_name_formatted.singularize.camelize%>.find_by(:id => params[:id])
19
- if @<%=module_name_formatted.singularize%>.blank?
20
- raise Spud::NotFoundError.new(:item => '<%= module_name_formatted.singularize %>')
21
- return false
22
- end
18
+ @<%=module_name_formatted.singularize%> = <%=module_name_formatted.singularize.camelize%>.find_by!(:id => params[:id])
23
19
  end
24
20
 
25
21
  end
@@ -0,0 +1,79 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe <%= controller_class_name %>, :type => :controller do
4
+
5
+ before(:each) do
6
+ # activate_session(admin: true)
7
+ end
8
+
9
+ describe 'index' do
10
+ it 'should render the index' do
11
+ get :index
12
+ expect(response).to have_http_status :success
13
+ expect(response).to render_template :index
14
+ end
15
+ end
16
+
17
+ describe 'show' do
18
+ it 'should render the show page' do
19
+ <%= model_variable_name %> = FactoryGirl.create(:<%= model_variable_name %>)
20
+ get :show, :id => <%= model_variable_name %>.id
21
+ expect(response).to have_http_status :success
22
+ expect(response).to render_template :show
23
+ end
24
+
25
+ it 'should return a 404 error' do
26
+ get :show, :id => 1
27
+ expect(response).to have_http_status :not_found
28
+ end
29
+ end
30
+
31
+ <%- if include_edit_actions? -%>
32
+ describe 'new' do
33
+ it 'should render the new page' do
34
+ get :new
35
+ expect(response).to have_http_status :success
36
+ expect(response).to render_template :new
37
+ end
38
+ end
39
+
40
+ describe 'edit' do
41
+ it 'should render the edit page' do
42
+ <%= model_variable_name %> = FactoryGirl.create(:<%= model_variable_name %>)
43
+ get :edit, :id => <%= model_variable_name %>.id
44
+ expect(response).to have_http_status :success
45
+ expect(response).to render_template :edit
46
+ end
47
+ end
48
+
49
+ describe 'create' do
50
+ it 'should create the record' do
51
+ expect{
52
+ post :create, :<%= model_variable_name %> => FactoryGirl.attributes_for(:<%= model_variable_name %>)
53
+ }.to change(<%= model_class_name %>, :count).by(1)
54
+ end
55
+ end
56
+
57
+ describe 'update' do
58
+ it 'should update the record' do
59
+ <%= model_variable_name %> = FactoryGirl.create(:<%= model_variable_name %>)
60
+ current_value = <%= model_variable_name %>.<%= model_string_attribute_name %>
61
+ new_value = current_value + '-Updated'
62
+ expect{
63
+ patch :update, :id => <%= model_variable_name %>.id, :<%= model_variable_name %> => {:<%= model_string_attribute_name %> => new_value}
64
+ <%= model_variable_name %>.reload
65
+ }.to change(<%= model_class_name %>, :<%= model_string_attribute_name %>).to(new_value)
66
+ end
67
+ end
68
+
69
+ describe 'destroy' do
70
+ it 'should destroy the record' do
71
+ <%= model_variable_name %> = FactoryGirl.create(:<%= model_variable_name %>)
72
+ expect{
73
+ delete :destroy, :id => <%= model_variable_name %>.id
74
+ }.to change(<%= model_class_name %>, :count).by(-1)
75
+ end
76
+ end
77
+ <%- end -%>
78
+
79
+ end
@@ -1,10 +1,10 @@
1
- <%%= form_for [:admin, @<%=module_name_formatted.singularize%>], :builder => TbCore::FormBuilder, :html => {:class => 'form-horizontal'} do |f| %>
1
+ <%%= tb_form_for [:admin, @<%=module_name_formatted.singularize%>], :remote => true, :data => {:errors => :inline, :success => admin_<%= module_name_formatted %>_path} do |f| %>
2
2
 
3
3
  <%%= tb_form_errors(f.object, :base) %>
4
4
  <%- attributes.collect{ |att| att.split(':') }.each do |arg_0, arg_1| %>
5
5
  <%= field_for_attribute(arg_1, arg_0) %>
6
6
  <%- end %>
7
- <%%= f.tb_save_buttons('<%= module_name_formatted.singularize.humanize.titlecase %>', @<%= module_name_formatted.singularize %>.new_record? ? admin_<%= module_name_formatted %>_path : admin_<%= module_name_formatted.singularize %>_path(@<%= module_name_formatted.singularize %>)) %>
7
+ <%%= f.tb_save_buttons('<%= module_name_formatted.singularize.humanize.titlecase %>', admin_<%= module_name_formatted %>_path) %>
8
8
 
9
9
  <%% end %>
10
10
 
@@ -4,7 +4,6 @@
4
4
  <meta charset="utf-8">
5
5
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1" />
7
- <meta name="viewport" content="width=device-width"/>
8
7
  <%%= tb_page_title() %>
9
8
  <%%= stylesheet_link_tag "application", :media => "all" %>
10
9
  <%%= javascript_include_tag "application" %>
@@ -16,12 +15,12 @@
16
15
 
17
16
  <%% if flash[:notice] %>
18
17
  <div class="alert alert-info alert-dismissible">
19
- <%%= flash[:notice] %> <a href="#" class="flash-remove">X</a>
18
+ <%%= flash[:notice] %>
20
19
  <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
21
20
  </div>
22
21
  <%% elsif flash[:error] %>
23
22
  <div class="alert alert-danger alert-dismissible">
24
- <%%= flash[:error] %> <a href="#" class="flash-remove">X</a>
23
+ <%%= flash[:error] %>
25
24
  <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
26
25
  </div>
27
26
  <%% end %>
@@ -29,7 +29,7 @@ module Spud::BelongsToApp
29
29
  elsif controller.action_name == 'edit' || controller.action_name == 'update'
30
30
  @page_name = "Edit #{@page_name.singularize}"
31
31
  elsif controller.action_name == 'show'
32
- @page_name = "Show #{@page_name.singularize}"
32
+ @page_name = "#{@page_name.singularize} Detail"
33
33
  end
34
34
  end
35
35
  end
@@ -70,3 +70,5 @@ module Spud
70
70
  end
71
71
  end
72
72
  end
73
+
74
+ TbCore::Engine = Spud::Core::Engine
@@ -2,30 +2,39 @@ class Spud::RequestError < StandardError
2
2
  attr_accessor :request_url, :item, :template
3
3
  attr_reader :code, :title
4
4
 
5
- def initialize(message)
5
+ # For compatability reasons, this method accepts multiple styles of inputs
6
+ # Going forward the expected input will be:
7
+ #
8
+ # * item: The item that could not be found. String. (default = page)
9
+ # * template (named): ERB template you wish to render
10
+ #
11
+ def initialize(item_or_opts='page', opts={})
12
+ if item_or_opts.is_a?(Hash)
13
+ # ActiveSupport::Deprecation.warn("Passing the :item as a key/value pair to #{self.class.to_s} is deprecated; Pass it as the first argument instead.")
14
+ @item = item_or_opts[:item]
15
+ @template = item_or_opts[:template]
16
+ else
17
+ @item = item_or_opts
18
+ @template = opts[:template]
19
+ end
6
20
  @template ||= 'layouts/error_page'
7
- super(message)
21
+ @title = I18n.t(:title, :scope => [:tb_core, :errors, @i18n])
22
+ super(I18n.t(:message, :scope => [:tb_core, :errors, @i18n], :item => @item))
8
23
  end
9
24
  end
10
25
 
11
26
  class Spud::AccessDeniedError < Spud::RequestError
12
- def initialize(opts={})
13
- @item = opts[:item] || 'page'
14
- @template = opts[:template]
27
+ def initialize(item_or_opts='page', opts={})
15
28
  @code = 403
16
- @title = "Access Denied"
17
- message = opts[:message] || "You are not authorized to view the requested #{item.downcase}."
18
- super(message)
29
+ @i18n = 'access_denied'
30
+ super(item_or_opts, opts)
19
31
  end
20
32
  end
21
33
 
22
34
  class Spud::NotFoundError < Spud::RequestError
23
- def initialize(opts={})
24
- @item = opts[:item] || 'page'
25
- @template = opts[:template]
35
+ def initialize(item_or_opts='page', opts={})
26
36
  @code = 404
27
- @title = "Not Found"
28
- message = opts[:message] || "The #{item.downcase} you were looking for could not be found."
29
- super(message)
37
+ @i18n = 'not_found'
38
+ super(item_or_opts, opts)
30
39
  end
31
40
  end
@@ -1,5 +1,5 @@
1
1
  module Spud
2
2
  module Core
3
- VERSION = "1.3.2"
3
+ VERSION = "1.3.3"
4
4
  end
5
5
  end
@@ -112,7 +112,7 @@ class TbCore::FormBuilder < ActionView::Helpers::FormBuilder
112
112
  def tb_save_buttons(model_name, cancel_path)
113
113
  content_tag :div, :class => 'form-group' do
114
114
  content_tag :div, :class => 'col-sm-offset-2 col-sm-10' do
115
- concat submit "Save #{model_name}", :class => 'btn btn-primary'
115
+ concat submit "Save #{model_name}", :class => 'btn btn-primary', :data => {:disable_with => "Saving #{model_name}...", :enable_with => 'Saved!'}
116
116
  concat ' '
117
117
  concat @template.link_to 'Cancel', cancel_path, :class => 'btn btn-default'
118
118
  end
@@ -149,6 +149,7 @@ class TbCore::FormBuilder < ActionView::Helpers::FormBuilder
149
149
  # Builds a text field group
150
150
  #
151
151
  def tb_text_field(attribute, options={})
152
+ options[:maxlength] ||= get_limit_for_column(attribute)
152
153
  tb_input_field(attribute, :text_field, options)
153
154
  end
154
155
 
@@ -262,4 +263,13 @@ private
262
263
  return :text_field
263
264
  end
264
265
 
266
+ def get_limit_for_column(attribute)
267
+ col = @object.class.columns.find{ |c| c.name == attribute.to_s }
268
+ if col.present? && col.sql_type.match(/^varchar/)
269
+ return col.limit
270
+ else
271
+ return nil
272
+ end
273
+ end
274
+
265
275
  end
@@ -0,0 +1,32 @@
1
+ class TbCore::Responder < ActionController::Responder
2
+
3
+ def initialize(*)
4
+ super
5
+
6
+ # Don't require a :location parameter for redirecting
7
+ if !@options.has_key?(:location)
8
+ @options[:location] = nil
9
+ end
10
+ end
11
+
12
+ # Override the common behavior for formats associated with APIs, such as :xml and :json.
13
+ #
14
+ def api_behavior
15
+ raise MissingRenderer.new(format) unless has_renderer?
16
+ if get?
17
+ display resource
18
+ elsif post? || patch?
19
+ display resource, :status => :created, :location => api_location
20
+ else
21
+ head :no_content
22
+ end
23
+ end
24
+
25
+ # Default responder only sends the basic errors array
26
+ # This override also sends the full messages for convenience
27
+ #
28
+ def json_resource_errors
29
+ return {:errors => resource.errors, :full_messages => resource.errors.full_messages}
30
+ end
31
+
32
+ end
@@ -0,0 +1,42 @@
1
+ require 'authlogic/test_case'
2
+ include Authlogic::TestCase
3
+
4
+ module TbCore::SessionHelper
5
+
6
+ # Use this helper in controller specs to establish a login session
7
+ # - admin: Set to true to create a super_admin
8
+ # - permissions: One or more permissions you want to assign to the user (a role will be auto generated)
9
+ #
10
+ def activate_session(admin: false, permissions: nil)
11
+ activate_authlogic()
12
+ if permissions
13
+ permissions = [permissions] unless permissions.is_a?(Array)
14
+ role = SpudRole.create(:name => 'New Role', :permission_tags => permissions)
15
+ else
16
+ role = nil
17
+ end
18
+ @user = FactoryGirl.create(:spud_user, {super_admin: admin, role: role})
19
+ SpudUserSession.create(@user)
20
+ return @user
21
+ end
22
+
23
+ # Returns the current user
24
+ #
25
+ def current_user
26
+ return @user
27
+ end
28
+ end
29
+
30
+ # Insert the session helper into RSpec if it is in use
31
+ #
32
+ if defined?(RSpec)
33
+ RSpec.configure do |config|
34
+ config.include TbCore::SessionHelper
35
+ end
36
+ end
37
+
38
+ # Auto load the spud_user factory if FactoryGirl is in use
39
+ #
40
+ if defined?(FactoryGirl)
41
+ Dir[TbCore::Engine.root.join('spec/factories/spud_user_factories.rb')].each{ |f| require f }
42
+ end