Permalink
Browse files

Re-factor project creation and test it a bit

Also fixed a couple of issues:
- feed:// URLs are automatically repaired
- Blog URL never un-hid when hosting changed away from Redmine
  • Loading branch information...
1 parent 93e7fa5 commit 33d4b6b013f2ff1a972b9798b5ce21c11f6159c5 @epall committed Feb 24, 2010
Showing with 159 additions and 100 deletions.
  1. +1 −40 app/controllers/project_controller.rb
  2. +63 −2 app/models/project.rb
  3. +66 −58 app/views/project/new.html.erb
  4. +29 −0 spec/models/project_spec.rb
@@ -2,46 +2,7 @@ class ProjectController < ApplicationController
layout 'application', :except => [:new, :create]
def create
- @project = Project.new
- @project.name = params[:name]
- @project.contributors = params[:contributors]
- @project.group = Group.last # TODO generalize
- @project.description = params[:description]
-
- if not params[:github].empty?
- @project.blog = params[:blog_url]
- @project.blog_feed = FeedDetector.fetch_feed_url(@project.blog) rescue ""
- components = params[:github].split('/')
- @project.source_code = "http://github.com/#{params[:github]}/"
- @project.source_code_feed = "http://github.com/feeds/#{components[0]}/commits/#{components[1]}/master"
- @project.wiki = "http://wiki.github.com/#{params[:github]}/"
- elsif not params[:redmine_home].empty?
- home = params[:redmine_home]
- name = home.split('/').last
- base = home.match("(https?://[^/]*)/.*")[1]
- @project.website = home
- @project.blog = "#{base}/projects/#{name}/news"
- @project.blog_feed = "#{base}/projects/#{name}/news?format=atom"
- @project.source_code = "#{base}/repositories/show/#{name}"
- @project.source_code_feed = "#{base}/repositories/show/#{name}?format=atom"
- @project.wiki = "#{base}/wiki/#{name}"
- elsif not params[:googlecode].empty?
- name = params[:googlecode]
- @project.blog = params[:blog_url]
- @project.blog_feed = FeedDetector.fetch_feed_url(@project.blog) rescue ""
- @project.source_code =
- @project.website = "http://code.google.com/p/#{name}/"
- @project.source_code = "http://code.google.com/p/#{name}/source/browse/"
- @project.source_code_feed = "http://code.google.com/feeds/p/#{name}/svnchanges/basic"
- @project.wiki = "http://code.google.com/p/#{name}/w/list"
- else # custom
- @project.blog = params[:blog_url]
- @project.blog_feed = FeedDetector.fetch_feed_url(@project.blog) rescue ""
- @project.source_code = params[:custom_url]
- @project.source_code_feed = FeedDetector.fetch_feed_url(@project.source_code) rescue ""
- @project.wiki = params[:custom_wiki]
- end
-
+ @project = Project.new(params[:project])
@project.password = generate_password
@project.approved = false
@project.save!
View
@@ -127,10 +127,71 @@ def last_source_code_entry
events.code.last
end
+ # BEGIN attribute customizations
+
+ # Safari generates feed:// links, which sucks
+ def source_code_feed=(feed)
+ super(feed.gsub(/^feed:/, 'http:'))
+ end
+
+ def blog_feed=(feed)
+ super(feed.gsub(/^feed:/, 'http:'))
+ end
+
+ # don't allow nulling out of fields to prevent overwrite
+ # of hosting type-derrived fields during construction
+ def source_code=(val)
+ super(val) if source_code.nil? || source_code == ''
+ end
+
+ def website=(val)
+ super(val) if website.nil? || website == ''
+ end
+
+ def wiki=(val)
+ super(val) if wiki.nil? || wiki == ''
+ end
+
+ # once set, you can't get back project hosting type
+ def github; ""; end
+ def googlecode; ""; end
+ def redmine; ""; end
+
+ # Support for various hosting types
+ def github=(project_name)
+ return if project_name.nil?
+ components = project_name.split('/')
+ self.source_code = "http://github.com/#{project_name}/"
+ self.source_code_feed = "http://github.com/feeds/#{components[0]}/commits/#{components[1]}/master"
+ self.wiki = "http://wiki.github.com/#{project_name}/"
+ end
+
+ def googlecode=(name)
+ return if name.nil?
+ self.website = "http://code.google.com/p/#{name}/"
+ self.source_code = "http://code.google.com/p/#{name}/source/browse/"
+ self.source_code_feed = "http://code.google.com/feeds/p/#{name}/svnchanges/basic"
+ self.wiki = "http://code.google.com/p/#{name}/w/list"
+ end
+
+ def redmine=(home)
+ return if home.nil?
+ name = home.split('/').last
+ base = home.match("(https?://[^/]*)/.*")[1]
+ self.website = home
+ self.blog = "#{base}/projects/#{name}/news"
+ self.blog_feed = "#{base}/projects/#{name}/news?format=atom"
+ self.source_code = "#{base}/repositories/show/#{name}"
+ self.source_code_feed = "#{base}/repositories/revisions/#{name}?format=atom"
+ self.wiki = "#{base}/wiki/#{name}"
+ end
+
+ # END attribute customizations
def before_save
- self.website = nil if self.website == ''
- self.wiki = nil if self.wiki == ''
+ # go find feeds
+ self.source_code_feed ||= FeedDetector.fetch_feed_url(self.source_code) rescue nil
+ self.blog_feed ||= FeedDetector.fetch_feed_url(self.blog) rescue nil
end
private
@@ -1,91 +1,99 @@
<% content_for :head do %>
- <%= javascript_include_tag 'jquery' %>
- <script type="text/javascript">
- $(function(){
- $('#hosting').change(function(){
- $('.hosting').hide();
- $(".hosting."+this.value.replace(" ", "_")).show();
-
- if(this.value == "Redmine"){
- $('#blog_info').hide();
- }
- });
- });
+<%= javascript_include_tag 'jquery' %>
+<script type="text/javascript">
+$(function(){
+ $('#hosting').change(function(){
+ $('.hosting').hide();
+ $(".hosting."+this.value.replace(" ", "_")).show();
+
+ if(this.value == "Redmine"){
+ $('#blog_info').hide();
+ } else {
+ $('#blog_info').show();
+ }
+ });
+});
</script>
<style type="text/css">
- tr.hosting {
- display: none;
- }
+tr.hosting {
+ display: none;
+}
+
+.hosting.custom {
+ display: table-row;
+}
- .hosting.custom {
- display: table-row;
- }
+table#project_entry th {
+ width: 20em;
+ text-align: right;
+ padding-right: 1em;
+}
- table#project_entry th {
- width: 20em;
- text-align: right;
- padding-right: 1em;
- }
+em {
+ font-size: 80%;
+ font-weight: normal;
+}
- em {
- font-size: 80%;
- font-weight: normal;
- }
+table {
+ margin: auto;
+ padding-right: 12em;
+}
- table {
- margin: auto;
- padding-right: 12em;
- }
-
</style>
<% end %>
<h1>New Project</h1>
-<% form_tag :action => 'create' do %>
- <table id="project_entry">
- <tr>
- <th><%= label_tag :name %></th>
- <td><%= text_field_tag :name %></td>
- </tr>
- <tr>
- <th><%= label_tag :contributors, "Contributors<br><em>(comma-separated)</em>" %></th>
- <td><%= text_field_tag :contributors %></td>
+<% form_for :project, Project.new, :url => {:action => :create} do |f| %>
+<table id="project_entry">
+ <tr>
+ <th><%= f.label :name %></th>
+ <td><%= f.text_field :name %></td>
+ </tr>
+ <tr>
+ <th><%= f.label :contributors, "Contributors<br><em>(comma-separated)</em>" %></th>
+ <td><%= f.text_field :contributors %></td>
<tr>
<th><%= label_tag :hosting %></th>
<td><%= select_tag :hosting, options_for_select(HostingType.enumerate) %></td>
</tr>
<tr class="hosting GitHub">
- <th><%= label_tag :github, "Project name<br><em>(e.g. epall/tracks)</em>" %> </th>
- <td><%= text_field_tag :github %></td>
+ <th><%= f.label :github, "Project name<br><em>(e.g. epall/tracks)</em>" %> </th>
+ <td><%= f.text_field :github %></td>
</tr>
<tr class="hosting Redmine">
- <th><label for="redmine_home">Redmine home<br>
- <em>(e.g. http://rpinventory.com/projects/show/rpinventory)</label></th>
- <td><%= text_field_tag :redmine_home %></td>
+ <th><%= f.label :redmine, "Redmine home" %><br>
+ <em>(e.g. http://rpinventory.com/projects/show/rpinventory)</em>
+ </th>
+ <td><%= f.text_field :redmine %></td>
</tr>
<tr class="hosting custom">
- <th><%= label_tag :custom_url, "Source Code URL" %></th>
- <td><%= text_field_tag :custom_url %></td>
+ <th><%= f.label :source_code, "Source Code URL" %></th>
+ <td><%= f.text_field :source_code %></td>
</tr>
<tr class="hosting custom">
- <th><%= label_tag :custom_wiki, "Wiki URL" %></th>
- <td><%= text_field_tag :custom_wiki %></td>
+ <th><%= f.label :wiki, "Wiki URL" %></th>
+ <td><%= f.text_field :wiki %></td>
</tr>
<tr class="hosting Google_Code">
- <th><label for="googlecode">Project Name<br><em>(e.g. dplms)</em></br></label></th>
- <td><%= text_field_tag :googlecode %></td>
+ <th><%= f.label :googlecode, "Project Name"%><br>
+ <em>(e.g. dplms)</em></label>
+ </th>
+ <td><%= f.text_field :googlecode %></td>
</tr>
-
+
<tr class="blog" id="blog_info">
- <th><%= label_tag :blog_url, "Blog URL<br><em>(e.g. http://ngodb.blogspot.com/)</em>" %></th>
- <td><%= text_field_tag :blog_url %></td>
+ <th><%= f.label :blog, "Blog URL"%><br>
+ <em>(e.g. http://ngodb.blogspot.com/)</em>
+ </th>
+ <td><%= f.text_field :blog %></td>
</tr>
-
+
<tr>
- <th><%= label_tag :description %></th>
- <td><%= text_area_tag :description, "", :rows => 10, :cols => 40 %></td>
+ <th><%= f.label :description %></th>
+ <td><%= f.text_area :description, :rows => 10, :cols => 40 %></td>
+ </tr>
<tr>
<td>&nbsp;</td>
@@ -0,0 +1,29 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe Project do
+ it "should translate feed:// to http://" do
+ p = Project.create(:name => "foo", :source_code_feed => "feed://foo.com/")
+ p.source_code_feed.should == "http://foo.com/"
+ end
+
+ it "should lookup feed if nil" do
+ p = Project.create(:name => "OSS Dashboard", :source_code => "http://github.com/epall/oss_dashboard/")
+ p.save!
+ p.source_code_feed.should_not be_nil
+ end
+
+ it "should not lookup feed if already specified" do
+ p = Project.create(:name => "OSS Dashboard",
+ :source_code => "http://github.com/epall/oss_dashboard/",
+ :source_code_feed => "http://github.com/epall.atom")
+ p.save!
+ p.source_code_feed.should == "http://github.com/epall.atom"
+ end
+
+ it "should automatically generate fields for GitHub hosting" do
+ p = Project.create(:name => "OSS Dashboard", :github => "epall/oss_dashboard")
+ p.source_code.should == "http://github.com/epall/oss_dashboard/"
+ p.source_code_feed.should == "http://github.com/feeds/epall/commits/oss_dashboard/master"
+ p.wiki.should == "http://wiki.github.com/epall/oss_dashboard/"
+ end
+end

0 comments on commit 33d4b6b

Please sign in to comment.