Skip to content
Browse files

Templates CRUD refactor

* Refactors Event/Embeds/Site Templates
* Remove deprecated controllers and views
* Added tests
  • Loading branch information...
1 parent 888f434 commit fd7becd9f6dda62ef99bac7f8e3823f11904c018 @elcuervo elcuervo committed Jun 18, 2012
View
1 Gemfile-deps
@@ -32,6 +32,7 @@ group :development do
gem 'looksee'
gem 'wirble'
gem 'sketches'
+ gem 'debugger'
end
group :development, :test do
View
9 Gemfile.lock
@@ -103,6 +103,7 @@ GEM
childprocess (0.3.1)
ffi (~> 1.0.6)
chronic (0.6.6)
+ columnize (0.3.6)
configatron (2.9.0)
yamler (>= 0.1.0)
cover_me (1.2.0)
@@ -111,6 +112,13 @@ GEM
crack (0.3.1)
daemons (1.1.8)
database_cleaner (0.7.1)
+ debugger (1.1.1)
+ columnize (>= 0.3.1)
+ debugger-linecache (~> 1.1)
+ debugger-ruby_core_source (~> 1.1)
+ debugger-linecache (1.1.1)
+ debugger-ruby_core_source (>= 1.1.1)
+ debugger-ruby_core_source (1.1.2)
devise (1.5.3)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.0.3)
@@ -299,6 +307,7 @@ DEPENDENCIES
database_cleaner (~> 0.6)
datajam-datacard!
datajam_chat!
+ debugger
devise (~> 1.1)
escape_utils (~> 0.2)
formatize (~> 1.0)
View
49 app/controllers/admin/templates/embeds_controller.rb
@@ -1,49 +0,0 @@
-class Admin::Templates::EmbedsController < AdminController
- before_filter :load_templates, only: [:index, :create]
-
- def load_templates
- @embed_templates = filter_and_sort EmbedTemplate.all.order(:updated_at => :desc)
- end
-
- def index
- @template = EmbedTemplate.new
- render_if_ajax 'admin/templates/embeds/_table'
- end
-
- def edit
- @template = EmbedTemplate.find(params[:id])
- end
-
- def create
- @template = EmbedTemplate.new(params[:templates_embed])
- if @template.save
- flash[:success] = "Template saved."
- redirect_to edit_admin_templates_embed_path(@template)
- else
- flash[:error] = @template.errors.full_messages.to_sentence
- render :index
- end
- end
-
- def update
- @template = EmbedTemplate.find(params[:id])
- if @template.update_attributes(params[:templates_embed])
- flash[:success] = "Template updated."
- redirect_to :back
- else
- flash[:error] = @template.errors.full_messages.to_sentence
- render :edit
- end
- end
-
- def destroy
- @template = EmbedTemplate.find(params[:id])
- if @template.destroy
- flash[:success] = "Template deleted."
- else
- flash[:error] = @template.errors.full_messages.to_sentence
- end
- redirect_to admin_templates_embeds_path
- end
-
-end
View
53 app/controllers/admin/templates/events_controller.rb
@@ -1,53 +0,0 @@
-class Admin::Templates::EventsController < AdminController
- before_filter :load_templates, only: [:index, :create]
-
- def load_templates
- @event_templates = filter_and_sort EventTemplate.all.order(:updated_at => :desc)
- end
-
- def index
- @template = EventTemplate.new
- render_if_ajax 'admin/templates/events/_table'
- end
-
- def edit
- @template = EventTemplate.find(params[:id])
- end
-
- def show
- redirect_to admin_templates_events_path
- end
-
- def create
- @template = EventTemplate.new(params[:templates_event])
- if @template.save
- flash[:success] = "Template saved."
- redirect_to edit_admin_templates_event_path(@template)
- else
- flash[:error] = @template.errors.full_messages.to_sentence
- render :index
- end
- end
-
- def update
- @template = EventTemplate.find(params[:id])
- if @template.update_attributes(params[:templates_event])
- flash[:success] = "Template updated."
- redirect_to :back
- else
- flash[:error] = @template.errors.full_messages.to_sentence
- render :edit
- end
- end
-
- def destroy
- @template = EventTemplate.find(params[:id])
- if @template.destroy
- flash[:success] = "Template deleted."
- else
- flash[:error] = @template.errors.full_messages.to_sentence
- end
- redirect_to admin_templates_events_path
- end
-
-end
View
11 app/controllers/admin/templates/site_controller.rb
@@ -1,16 +1,15 @@
class Admin::Templates::SiteController < AdminController
-
- def show
- @site_template = SiteTemplate.first
+ def index
+ @template = SiteTemplate.first
end
def update
- @site_template = SiteTemplate.first
- if @site_template.update_attributes(params[:templates_site])
+ @template = SiteTemplate.first
+ if @template.update_attributes(params[:templates_site])
flash[:success] = "Template updated."
redirect_to :back
else
- flash[:error] = @site_template.errors.full_messages.to_sentence
+ flash[:error] = @template.errors.full_messages.to_sentence
redirect_to :back
end
end
View
45 app/controllers/admin/templates_controller.rb
@@ -1,50 +1,61 @@
class Admin::TemplatesController < AdminController
+ before_filter :load_templates, only: [:index]
- def index
- @template = Template.new
- @site_template = SiteTemplate.first
- @event_templates = EventTemplate.all
- @embed_templates = EmbedTemplate.all
+ def load_type
+ @type = params[:type] || (params[:templates_event][:type] || params[:templates_embed][:type]).constantize
end
- def show
- @template = Template.find(params[:id])
+ def load_templates
+ load_type
+ @templates = filter_and_sort @type.all.order(:updated_at => :desc)
+ end
+
+ def possible_params
+ params[:templates_event] || params[:templates_embed] || params[:templates_site]
+ end
+
+ def index
+ @template = @type.new
+ render_if_ajax 'admin/templates/_table'
end
- def new
- @template = Template.new
+ def show
+ redirect_to admin_templates_path(@type)
end
def edit
@template = Template.find(params[:id])
end
def create
- @template = Template.new(params[:template])
+ load_type
+ @template = @type.new(possible_params)
if @template.save
flash[:success] = "Template saved."
- redirect_to admin_templates_path
else
flash[:error] = @template.errors.full_messages.to_sentence
- redirect_to admin_templates_path
end
+ redirect_to :back
end
def update
@template = Template.find(params[:id])
- if @template.update_attributes(params)
+ if @template.update_attributes(possible_params)
flash[:success] = "Template updated."
redirect_to :back
else
flash[:error] = @template.errors.full_messages.to_sentence
- redirect_to :back
+ render :edit
end
end
def destroy
@template = Template.find(params[:id])
- @template.destroy
- redirect_to admin_templates_path
+ if @template.destroy
+ flash[:success] = "Template deleted."
+ else
+ flash[:error] = @template.errors.full_messages.to_sentence
+ end
+ redirect_to :back
end
-
end
View
4 app/views/admin/templates/_form.html.erb
@@ -1,5 +1,7 @@
-<%= simple_form_for [:admin, @template], html: { class: "form-horizontal" } do |f| %>
+<% url = @template.persisted? ? admin_template_path(@template) : admin_templates_path %>
+<%= simple_form_for @template, url: url, html: { class: "form-horizontal" } do |f| %>
<%= f.input :name %>
+ <%= f.input :type, as: :hidden, input_html: { value: @template._type } %>
<%= f.input :template, as: :text, input_html: { class: "datajamTemplate", rows: 20, cols: 100 } %>
<% if @template.custom_fields.any? %>
View
4 app/views/admin/templates/_table.html.erb
@@ -0,0 +1,4 @@
+<%= search_box(:name) %>
+<%= table_for @templates, [:name, :updated_at, ""] do |template| %>
+ <%= render :partial => "template", locals: { template: template } %>
+<% end %>
View
5 app/views/admin/templates/_template.erb
@@ -1,9 +1,10 @@
<%= content_tag_for :tr, template do %>
- <td><%= link_to template.name, [:edit, :admin, template] %></td>
+ <td><%= link_to template.name, edit_admin_template_path(template) %></td>
+
<td><%= template.updated_at.to_s :humane %></td>
<td>
<% if template.can_be_deleted? %>
- <%= delete_button admin_templates_embed_path(template), confirm: "Are you sure? This will completely destroy the template." %>
+ <%= delete_button admin_template_path(template), confirm: "Are you sure? This will completely destroy the template." %>
<% else %>
<button class="btn disabled btn-danger btn-small">Delete</button>
<% end %>
View
0 ...iews/admin/templates/events/edit.html.erb → app/views/admin/templates/edit.html.erb
File renamed without changes.
View
4 app/views/admin/templates/embeds/_table.html.erb
@@ -1,4 +0,0 @@
-<%= search_box(:name) %>
-<%= table_for @embed_templates, [:name, :updated_at, ""] do |template| %>
- <%= render "admin/templates/template", template: template %>
-<% end %>
View
2 app/views/admin/templates/embeds/edit.html.erb
@@ -1,2 +0,0 @@
-<h2>Edit Template</h2>
-<%= render 'admin/templates/form' %>
View
15 app/views/admin/templates/embeds/index.html.erb
@@ -1,15 +0,0 @@
-<h2>Embed Templates</h2>
-
-<ul class="nav nav-pills">
- <li class="active"><a href="#index" data-toggle="pill">List</a></li>
- <li><a href="#new" data-toggle="pill">Create New</a></li>
-</ul>
-<div class="tab-content">
- <div class="tab-pane active" id="index">
- <%= render :partial => 'admin/templates/embeds/table' %>
- </div>
-
- <div class="tab-pane" id="new">
- <%= render "admin/templates/form" %>
- </div>
-</div>
View
4 app/views/admin/templates/events/_table.html.erb
@@ -1,4 +0,0 @@
-<%= search_box(:name) %>
-<%= table_for @event_templates, [:name, :updated_at, ""] do |template| %>
- <%= render "admin/templates/template", template: template %>
-<% end %>
View
15 app/views/admin/templates/events/index.html.erb
@@ -1,15 +0,0 @@
-<h2>Event Templates</h2>
-
-<ul class="nav nav-pills">
- <li class="active"><a href="#index" data-toggle="pill">List</a></li>
- <li><a href="#new" data-toggle="pill">Create New</a></li>
-</ul>
-<div class="tab-content">
- <div class="tab-pane active" id="index">
- <%= render :partial => 'admin/templates/events/table' %>
- </div>
-
- <div class="tab-pane" id="new">
- <%= render "admin/templates/form" %>
- </div>
-</div>
View
15 app/views/admin/templates/index.html.erb
@@ -0,0 +1,15 @@
+<h2><%= @template._type %></h2>
+
+<ul class="nav nav-pills">
+ <li class="active"><a href="#index" data-toggle="pill">List</a></li>
+ <li><a href="#new" data-toggle="pill">Create New</a></li>
+</ul>
+<div class="tab-content">
+ <div class="tab-pane active" id="index">
+ <%= render 'table' %>
+ </div>
+
+ <div class="tab-pane" id="new">
+ <%= render 'form' %>
+ </div>
+</div>
View
9 app/views/admin/templates/site/index.html.erb
@@ -0,0 +1,9 @@
+<h2>Site Template</h2>
+<div class="well">
+ <p>Define the site template below. Upload related images and stylesheets in the <a href="/admin/assets">assets</a> area or your own CDN.</p>
+ <ul>
+ <li>Insert <code>{{{content}}}</code> somewhere into the template. The proper event or page content will be rendered inside.</li>
+ <li>Insert <code>{{{head_assets}}}</code> and <code>{{{body_assets}}}</code> before the closing <code>&lt;/head&gt;</code> and <code>&lt;/body&gt;</code> tags respectively.</li>
+ </ul>
+</div>
+<%= render :partial => 'admin/templates/form' %>
View
16 app/views/admin/templates/site/show.html.erb
@@ -1,16 +0,0 @@
-<h2>Site Template</h2>
-<div class="well">
- <p>Define the site template below. Upload related images and stylesheets in the <a href="/admin/assets">assets</a> area or your own CDN.</p>
- <ul>
- <li>Insert <code>{{{content}}}</code> somewhere into the template. The proper event or page content will be rendered inside.</li>
- <li>Insert <code>{{{head_assets}}}</code> and <code>{{{body_assets}}}</code> before the closing <code>&lt;/head&gt;</code> and <code>&lt;/body&gt;</code> tags respectively.</li>
- </ul>
-</div>
-
-<%= simple_form_for [:admin, @site_template], html: { class: "form-horizontal" } do |f| %>
- <%= f.input :template, as: :text, input_html: { class: 'datajamTemplate', rows: 20, cols: 100 } %>
- <div class="form-actions">
- <%= f.button :submit, label: "Update", class: "btn-primary" %>
- </div>
-<% end %>
-
View
6 config/navigation.rb
@@ -11,9 +11,9 @@
Datajam.navigation << {
title: 'Templates',
items: [
- { title: 'Event templates', url: '/admin/templates/events', icon: 'edit' },
- { title: 'Embed templates', url: '/admin/templates/embeds', icon: 'edit' },
- { title: 'Site template', url: '/admin/templates/site', icon: 'edit' },
+ { title: 'Event templates', url: '/admin/events_templates', icon: 'edit' },
+ { title: 'Embed templates', url: '/admin/embeds_templates', icon: 'edit' },
+ { title: 'Site template', url: '/admin/site_templates', icon: 'edit' },
]
}
View
11 config/routes.rb
@@ -17,11 +17,12 @@
resources :reminders, :only => :destroy
end
resources :pages
- namespace :templates do
- resource :site, :controller => 'site'
- resources :events
- resources :embeds
- end
+ resources :cards
+ resources :templates
+
+ resources :site_templates, :controller => 'templates/site', :type => SiteTemplate, :as => 'templates_site'
+ resources :events_templates, :controller => 'templates', :type => EventTemplate, :as => 'templates_events'
+ resources :embeds_templates, :controller => 'templates', :type => EmbedTemplate, :as => 'templates_embeds'
end
resources :reminders, :only => [:create]
View
13 spec/acceptance/acceptance_helper.rb
@@ -1,3 +1,16 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
require 'capybara/rspec'
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
+
+def create_admin
+ User.create!(:email => 'test@test.com', :password => 'password')
+end
+
+def login_as_admin
+ visit '/admin'
+ page.should have_content('Sign in')
+ fill_in 'Email', :with => 'test@test.com'
+ fill_in 'Password', :with => 'password'
+ click_button 'Sign in'
+ page.should have_content('Signed in successfully')
+end
View
27 spec/acceptance/admin_spec.rb
@@ -1,18 +1,27 @@
require File.expand_path(File.dirname(__FILE__) + '/acceptance_helper')
feature 'Admin area' do
+ background {
+ create_admin
+ }
- background do
- User.create!(:email => 'test@test.com', :password => 'password')
+ scenario 'Sign in' do
+ login_as_admin
end
- scenario 'Sign in' do
- visit '/admin'
- page.should have_content('Sign in')
- fill_in 'Email', :with => 'test@test.com'
- fill_in 'Password', :with => 'password'
- click_button 'Sign in'
- page.should have_content('Signed in successfully')
+ scenario 'Viewing event templates' do
+ login_as_admin
+
+ visit 'admin/events_templates'
+ page.has_link? "#new"
+ page.has_link? "#index"
end
+ scenario 'Viewing embeds templates' do
+ login_as_admin
+
+ visit 'admin/embeds_templates'
+ page.has_link? "#new"
+ page.has_link? "#index"
+ end
end
View
2 spec/controllers/admin_controller_spec.rb
@@ -1,5 +1,7 @@
require 'spec_helper'
describe AdminController do
+ it 'should' do
+ end
end

0 comments on commit fd7becd

Please sign in to comment.
Something went wrong with that request. Please try again.