Skip to content

Commit

Permalink
able to add categories when adding a new article, added features, eve…
Browse files Browse the repository at this point in the history
…rything works
  • Loading branch information
scottkf committed Oct 21, 2010
1 parent 0d0b161 commit 868fd18
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 43 deletions.
1 change: 1 addition & 0 deletions app/controllers/articles_controller.rb
Expand Up @@ -12,6 +12,7 @@ def index

def show
@article = Article.find(params[:id])
render :layout => "#{@article.category.layout}"
end

def create
Expand Down
14 changes: 8 additions & 6 deletions app/controllers/categories_controller.rb
Expand Up @@ -10,6 +10,7 @@ def show
end

def new
render :layout => nil if params[:js]
end

def edit
Expand All @@ -21,16 +22,17 @@ def create
if @category.save
flash[:notice] = "The category has been created"
respond_to do |format|
format.json { render :json => @category.to_json, :status => 200 }
format.js
format.xml { head :ok }
format.html { redirect_to categories_url }
end
else
respond_to do |format|
format.json { render :text => "Could not create category", :status => :unprocessable_entity } # placeholder
format.xml { head :ok }
format.html { render :action => :new, :status => :unprocessable_entity }
end
# render :action => :new
# respond_to do |format|
# format.json { render :text => "Could not create category", :status => :unprocessable_entity } # placeholder
# format.xml { head :ok }
# format.html { render :action => :new, :status => :unprocessable_entity }
# end
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/models/category.rb
Expand Up @@ -10,8 +10,8 @@ class Category < ActiveRecord::Base

before_save :layout_exists

validates_format_of :url, :with => /\A[a-zA-Z0-9]+\z/, :message => "No special characters are allowed"
validates_length_of :url, :in => 3..10, :too_short => "Must have atleast 3 characters", :too_long => "Must have 10 or fewers characters"
validates_format_of :url, :with => /\A[a-zA-Z0-9]+\z/, :message => "cannot have special characters"
validates_length_of :url, :in => 3..10, :too_short => "must have atleast 3 characters", :too_long => "must have 10 or fewers characters"

private
def layout_exists
Expand Down
3 changes: 1 addition & 2 deletions app/views/articles/_form.html.erb
Expand Up @@ -15,9 +15,8 @@
<%= field_set_tag do %>
<%= f.label Category %>
<%= link_to_if(can?(:manage, Category), "add a new category", new_category_path + "?js=true", :remote => true, :class => 'js') {} %>
<%= link_to_if(can?(:create, Category), "add a new category", new_category_path + "?js=true", :rel => "ajax", :class => 'js') {} %>
<%= f.select :category_id, @categories %>
<% end %>
<div id="add_category"></div>
<%= submit_tag (params[:action] == 'index' && 'Post') || 'Edit', :id => "submit" %>
<% end %>
4 changes: 2 additions & 2 deletions app/views/categories/_form.html.erb
@@ -1,6 +1,6 @@
<%= form_for @category, :remote => (params[:js]) || false do |f| %>
<% if @category.errors.any? -%>
<%= render :partial => 'shared/errors', :locals => {:object => @article} %>
<%= render :partial => 'shared/errors', :locals => {:object => @category} %>
<% end %>
<%= field_set_tag do %>
<%= f.label :name%>
Expand All @@ -22,5 +22,5 @@
<p>Just the basename, i.e.: "layout", would use layouts/layout.html.erb</p>
<%= f.text_field :layout %>
<% end %>
<%= submit_tag (params[:action] == 'new' && 'Create') || 'Edit', :id => "submit" %>
<%= submit_tag ((params[:action] == 'new' or params[:action] == 'create') && 'Create') || 'Edit', :id => "submit" %>
<% end %>
34 changes: 11 additions & 23 deletions app/views/categories/create.js.erb
@@ -1,34 +1,22 @@
if ($("#category_errors").length == 0) $("#new_article").prepend("<ul id='category_errors'></ul>")
if ($(".notice").length == 0) $("body").prepend("<div class='notice'></div>");
if ($("#errors").length == 0) $("#new_category").prepend("<ul id='errors'></ul>");
<% if @category.errors.any? -%>
/*Hide the flash notice div*/
$(".notice").hide(300);


$("#category_errors").replaceWith("<%= escape_javascript(render :partial => 'shared/errors', :locals => {:object => @category})%>");
$("#category_errors").hide(0);
$("#category_errors").show(300);
$("#errors").replaceWith("<%= escape_javascript(render :partial => 'shared/errors', :locals => {:object => @category})%>");
$("#errors").hide(0);
$("#errors").show(300);

<% else -%>
$("#category_errors").hide(300);

/*Update the html of the div flash_notice with the new one*/
$(".notice").html("<%= escape_javascript(flash[:notice])%>");

/*Show the flash_notice div*/
$(".notice").show(300);
$("#errors").hide(300);

/*Clear the entire form*/
$(":input:not(input[type = submit])").val("");

/*Remove the entire form*/
$('#new_article').remove();
/*Remove the entire form and close colorbox*/
$.colorbox.close();

$('#article_category_id').append(
$('<option></option>').val("<%= escape_javascript(@category.id) %>").html("<%= escape_javascript(@category.name) %>")
$('<option></option>')
.val("<%= @category.id %>")
.html("<%= escape_javascript(@category.name) %>")
.attr('selected', 'selected')
);
<% flash[:notice] = '' %>

$(".notice").delay(3000).hide(300);

<% end -%>
3 changes: 0 additions & 3 deletions app/views/categories/new.js.erb

This file was deleted.

21 changes: 16 additions & 5 deletions features/category.feature
Expand Up @@ -86,9 +86,19 @@ Feature: Category

@individualarticle
Scenario: Viewing an individual article should be shown with the layout
Given context
When event
Then outcome
Given I am not authenticated
# it shouldn't matter --^
And I have an article
And the article has a title "hello"
And the article has a body "....L"
And the article has a category with name "DancingBeetle" and url "beetle"
And the article has an author "steve" "brown"
And the layout has "<html><body><h1>UNCOMMONSHIBBOLETH</h1><%= yield %></body></html>"
When I go to the article page for "hello"
Then I should see "UNCOMMONSHIBBOLETH" within "h1"
And I should see "hello"
And I should see "....L"
And show me the page



Expand All @@ -104,15 +114,16 @@ Feature: Category
And the article has a category with name "DancingBeetle"
And I authored the article
And I go to the homepage
When I am on the homepage
When I am on the homepage
And I follow "Edit this article"
And I follow "add a new category"
And I wait until "new_category" is visible
And I fill in "category[name]" with "hello"
And I fill in "category[description]" with "goodbye"
And I fill in "category[url]" with "hello2"
And I fill in "category[layout]" with "hello"
And I press "submit"
Then outcome
Then "hello" should be selected for "article_category_id"



Expand Down
6 changes: 6 additions & 0 deletions features/step_definitions/category_steps.rb
Expand Up @@ -11,3 +11,9 @@
Given /^the layout has "([^"]*)"$/ do |content|
File.open(Rails.root.join("app", "views", "layouts","#{@category.layout}.html.erb"), 'w') {|f| f.write(content) }
end


When /^I wait until "([^"]*)" is visible$/ do |selector|
page.has_css?("#{selector}", :visible => true)
end

4 changes: 4 additions & 0 deletions features/step_definitions/web_steps.rb
Expand Up @@ -146,6 +146,10 @@ def with_scope(locator)
end
end

Then /^"([^"]*)" should be selected for "([^"]*)"$/ do |value, field|
assert page.has_xpath?("//option[@selected = 'selected' and contains(string(), value)]")
end

Then /^the "([^"]*)" field(?: within "([^"]*)")? should contain "([^"]*)"$/ do |field, selector, value|
with_scope(selector) do
field = find_field(field)
Expand Down
3 changes: 3 additions & 0 deletions features/support/paths.rb
Expand Up @@ -23,6 +23,9 @@ def path_to(page_name)
when /the (.*) category page/
articles_category_url($1)

when /the article page for "([^"]*)"/
article_path(Article.find_by_title($1))

when /register/
'/users/sign_up'

Expand Down
1 change: 1 addition & 0 deletions public/javascripts/application.js
Expand Up @@ -7,6 +7,7 @@ $(document).ready(function() {
if ($(value)) $(value).delay(5000).slideUp(300);
});

jQuery("a[rel='ajax']").colorbox({transition: "elastic", width: "55%", rel: "nofollow"})
jQuery("a[rel='markdown']").colorbox({transition:"elastic", width:"55%", rel:"nofollow"});

});

0 comments on commit 868fd18

Please sign in to comment.