Permalink
Browse files

Moved to lib -> ns -> function through rels. Fixed failing tests.

  • Loading branch information...
1 parent 2a00616 commit 7ff50be8656d6f35bd7c8b37f5bc5377b0400751 @zk committed Oct 10, 2010
View
@@ -15,5 +15,8 @@ gem 'levenshtein', '0.2.0'
group :test do
gem "shoulda", "2.11.3"
gem "flexmock", "0.8.7"
+ gem "ZenTest", "4.4.0"
+ gem "autotest-fsevent", "0.2.3"
+ gem "autotest-growl", "0.2.6"
gem "rcov", "0.9.9", :require => nil
end
View
@@ -1,6 +1,7 @@
GEM
remote: http://rubygems.org/
specs:
+ ZenTest (4.4.0)
actionmailer (2.3.5)
actionpack (= 2.3.5)
actionpack (2.3.5)
@@ -11,6 +12,9 @@ GEM
activeresource (2.3.5)
activesupport (= 2.3.5)
activesupport (2.3.5)
+ autotest-fsevent (0.2.3)
+ sys-uname
+ autotest-growl (0.2.6)
chronic (0.2.3)
hoe (>= 1.2.1)
flexmock (0.8.7)
@@ -39,12 +43,16 @@ GEM
json_pure (>= 1.1.7)
rubytree (0.7.0)
shoulda (2.11.3)
+ sys-uname (0.8.4)
will_paginate (2.3.12)
PLATFORMS
ruby
DEPENDENCIES
+ ZenTest (= 4.4.0)
+ autotest-fsevent (= 0.2.3)
+ autotest-growl (= 0.2.6)
chronic (= 0.2.3)
flexmock (= 0.8.7)
levenshtein (= 0.2.0)
@@ -42,21 +42,24 @@ def find_recently_updated(size, lib = nil)
examples = Example.find(:all, :limit => size, :order => 'updated_at DESC')
see_alsos = SeeAlso.find(:all, :limit => size, :order => 'updated_at DESC')
else
- comments = Comment.find(:all,
- :joins => "INNER JOIN functions ON comments.commentable_id = functions.id",
- :conditions => ["functions.library = ?", lib],
- :limit => size,
- :order => 'updated_at DESC')
- examples = Example.find(:all,
- :joins => "INNER JOIN functions ON examples.function_id = functions.id",
- :conditions => ["functions.library = ?", lib],
- :limit => size,
- :order => 'updated_at DESC')
- see_alsos = SeeAlso.find(:all,
- :joins => "INNER JOIN functions ON see_alsos.from_id = functions.id",
- :conditions => ["functions.library = ?", lib],
+
+ comments = Comment.find(:all,
+ :joins => "INNER JOIN functions ON comments.commentable_id = functions.id LEFT JOIN namespaces ON functions.namespace_id = namespaces.id LEFT JOIN libraries ON namespaces.library_id = libraries.id",
+ :conditions => ["libraries.name = ?", lib],
:limit => size,
:order => 'updated_at DESC')
+
+ examples = Example.find(:all,
+ :include => [:function, {:function => :namespace}, {:function => {:namespace => :library}}],
+ :conditions => {:functions => {:namespaces => {:libraries => {:name => lib}}}},
+ :limit => size,
+ :order => 'examples.updated_at DESC')
+
+ see_alsos = SeeAlso.find(:all,
+ :include => [:from_function, {:from_function => :namespace}, {:from_function => {:namespace => :library}}],
+ :conditions => {:from_functions => {:namespaces => {:libraries => {:name => lib}}}},
+ :limit => size,
+ :order => 'see_alsos.updated_at DESC')
end
recent = (comments + examples + see_alsos).sort{|a,b| b.updated_at <=> a.updated_at}
@@ -146,14 +146,11 @@ def ns
@library = Library.find_by_url_friendly_name_and_current(lib_name, true)
end
- @ns = nil
- if version
- @ns = Namespace.find_by_name_and_version(ns_name, version)
- else
- @ns = Namespace.find_by_name(ns_name)
+ @ns = nil
+ if @library
+ @ns = Namespace.find_by_name_and_library_id(ns_name, @library.id)
end
-
if not @ns or not @library
render :template => 'public/404.html', :layout => false, :status => 404
return
@@ -167,42 +164,25 @@ def function
ns = params[:ns]
function_url_name = params[:function]
- @library = nil
- if version
- @library = Library.find_by_url_friendly_name_and_version(lib_url_name, version)
- else
- @library = Library.find_by_url_friendly_name_and_current(lib_url_name, true)
- end
-
- @ns = Namespace.find_by_name(ns)
@function = Function.find(
- :first,
+ :first,
+ :include => [:namespace, {:namespace => :library}],
:conditions => {
- :library => @library.name,
- :version => @library.version,
- :ns => ns,
+ :namespaces => {:name => ns, :libraries => {:url_friendly_name => lib_url_name}},
:url_friendly_name => function_url_name}
)
-
+
if not @function
logger.error "Couldn't find function id #{params[:id]}"
render :template => 'public/404.html', :layout => false, :status => 404
return
end
- if not @library
- logger.error "Couldn't find library by function #{@function.to_yaml}"
-
- render :template => 'public/404.html', :layout => false, :status => 404
- return
- end
-
@example = Example.new
@comment = Comment.new
if request.post?
-
if params[:update_comment]
@comment = Comment.find(params[:comment_id])
if @comment and @comment.user_id == current_user.id
@@ -225,7 +205,13 @@ def function
def function_short_link
@function = Function.find(params[:id]) rescue nil
- @library = Library.find_by_name_and_version(@function.library, @function.version)
+
+ if not @function
+ logger.error "Couldn't find function id #{params[:id]}"
+
+ render :template => 'public/404.html', :layout => false, :status => 404
+ return
+ end
version = (params[:version] || @function.version)
@@ -238,9 +224,9 @@ def function_short_link
redirect_to :controller => 'main',
:action => 'function',
- :lib => @library.url_friendly_name,
- :version => (@library.current ? nil : @library.version),
- :ns => @function.ns,
+ :lib => @function.namespace.library.url_friendly_name,
+ :version => (@function.namespace.library.current ? nil : @function.namespace.library.version),
+ :ns => @function.namespace.name,
:function => @function.url_friendly_name
end
@@ -57,7 +57,7 @@ def lookup
@functions = @functions[0, 10]
end
- render :json => @functions.map{|f| {:href => f.href, :ns => f.ns, :name => f.name, :examples => f.examples.size, :shortdoc => f.shortdoc }}
+ render :json => @functions.map{|f| {:href => f.href, :ns => f.namespace.name, :name => f.name, :examples => f.examples.size, :shortdoc => f.shortdoc }}
end
def delete
@@ -112,7 +112,8 @@ def add
ns = split[0]
name = split[1]
- to_var = Function.find_by_ns_and_name(ns, name)
+ to_var = Function.find(:first, :include => [:namespace],
+ :conditions => {:namespaces => {:name => ns}, :name => name})
if not to_var
render json_fail("Couldn't find to var.")
@@ -136,7 +137,7 @@ def add
:to_var => {
:sa_id => sa.id,
:name => to_var.name,
- :ns => to_var.ns,
+ :ns => to_var.namespace.name,
:href => to_var.href,
:shortdoc => to_var.shortdoc
},
View
@@ -1,4 +1,6 @@
class Function < ActiveRecord::Base
+ belongs_to :namespace
+
has_many :examples
has_many :comments
has_and_belongs_to_many :source_references,
@@ -43,6 +45,10 @@ def href
"/v/#{id}"
end
+ def library
+ namespace.library
+ end
+
def see_alsos_sorted
see_alsos.sort{|a,b| b.vote_score <=> a.vote_score}
end
@@ -51,32 +57,20 @@ def self.libraries
Function.find(:all, :select => 'distinct(library),library').map(&:library).sort
end
- def self.in_library(lib)
- Function.find(:all, :conditions => {:library => lib.name, :version => lib.version}, :select => 'library,ns,name, weight, id', :order => 'name ASC, weight DESC')
- end
-
- def self.in_library_and_ns(lib, ns)
- Function.find(:all, :conditions => {:library => lib.name, :ns => ns, :version => lib.version}, :order => 'name ASC, weight DESC')
- end
-
def self.versions_of(function)
- Function.find(:all, :conditions => {:library => function.library_.name,
- :ns => function.ns,
- :name => function.name})
- end
-
- # not ready to make the leap to a has_many / belongs_to yet, so this
- # will have to do for now
- def library_
- Library.find_by_name_and_version(library, version)
+ Function.find(:all,
+ :include => [:namespace, {:namespace => :library}],
+ :conditions => {:namespaces => {:name => function.namespace.name,
+ :libraries => {:name => function.library.name}},
+ :name => function.name})
end
def link_opts(use_current_vs_actual_version = true)
{:controller => 'main',
:action => 'function',
- :lib => library_.url_friendly_name,
- :version => (use_current_vs_actual_version && library_.current ? nil : version),
- :ns => ns,
+ :lib => library.url_friendly_name,
+ :version => (use_current_vs_actual_version && library.current ? nil : version),
+ :ns => namespace.name,
:function => url_friendly_name}
end
View
@@ -1,6 +1,7 @@
class Library < ActiveRecord::Base
+
has_many :namespaces
-
+
#:nocov:
define_index do
indexes :name
View
@@ -1,3 +1,9 @@
class Namespace < ActiveRecord::Base
belongs_to :library
+ has_many :functions
+
+ def self.versions_of(ns)
+ Namespace.find(:all, :conditions => {:name => ns.name})
+ end
+
end
@@ -9,7 +9,7 @@ current_ns = params[:ns]
if @library
require 'tree'
- _nss = Function.find(:all, :conditions => {:library => @library.name}, :select => 'distinct(ns),ns').map(&:ns).sort
+ _nss = @library.namespaces.map(&:name).sort
@ns_flat_list = _nss
@@ -1,6 +1,10 @@
<%
-total_count = Function.find_by_sql(["select count(id) from functions where library = ?", @lib.name])[0]["count(id)"].to_i
-missing_examples = Function.find_by_sql(["select distinct id, library, ns, name from functions where (id) not in (select function_id from examples) and library = ? order by name", @lib.name])
+#total_count = Function.find_by_sql(["select count(id) from functions where library = ?", @lib.name])[0]["count(id)"].to_i
+total_count = Function.count(:all,
+ :include => [:namespace, {:namespace => :library}],
+ :conditions => {:namespaces => {:libraries => {:name => @lib.name}}})
+
+missing_examples = Function.find_by_sql(["select distinct name, id from functions where (id) not in (select function_id from examples) order by name"])
missing_examples_count = missing_examples.size
functions_with_examples_count = total_count - missing_examples_count
@@ -30,7 +34,7 @@ groups = functions_group_into_alpha(missing_examples)
<h5><a name="missing_examples_<%= gs[0] %>"><%= gs[0] %></a></h5>
<ul>
<% gs[1].each do |f| %>
- <li><a href="<%= f.href %>"><span class="ns"><%= f.ns %>/</span><%= f.name %></a></li>
+ <li><a href="<%= f.href %>"><%= f.name %></a></li>
<% end %>
</ul>
<% end %>
@@ -8,7 +8,7 @@ recently_updated.each do |u|
<%= link_to u.user.login, :controller => 'user', :action => 'profile', :login => u.user.login %>
</span>
<%= (u.created_at == u.updated_at) ? "commented on" : "updated a comment on" %>
- <a href="<%= func.href %>"><span class="ns"><%= func.ns %>/</span><span class="name"><%= func.name %></span></a>
+ <a href="<%= func.href %>"><span class="ns"><%= func.namespace.name %>/</span><span class="name"><%= func.name %></span></a>
<%= time_ago_or_time_stamp(u.updated_at) %>
</div>
@@ -20,7 +20,7 @@ recently_updated.each do |u|
<%= link_to u.user.login, :controller => 'user', :action => 'profile', :login => u.user.login %>
</span>
<%= (u.created_at == u.updated_at) ? "added an example to" : "updated an example on" %>
- <a href="<%= func.href %>"><span class="ns"><%= func.ns %>/</span><span class="name"><%= func.name %></span></a>
+ <a href="<%= func.href %>"><span class="ns"><%= func.namespace.name %>/</span><span class="name"><%= func.name %></span></a>
<%= time_ago_or_time_stamp(u.updated_at)%>
</div>
@@ -32,7 +32,7 @@ recently_updated.each do |u|
<%= link_to u.user.login, :controller => 'user', :action => 'profile', :login => u.user.login %>
</span>
<%= (u.created_at == u.updated_at) ? "added a see also to" : "updated a see also on" %>
- <a href="<%= func.href %>"><span class="ns"><%= func.ns %>/</span><span class="name"><%= func.name %></span></a>
+ <a href="<%= func.href %>"><span class="ns"><%= func.namespace.name %>/</span><span class="name"><%= func.name %></span></a>
<%= time_ago_or_time_stamp(u.updated_at)%>
</div>
<% end %>
@@ -1,7 +1,7 @@
<script type="text/javascript" charset="utf-8">
SyntaxHighlighter.all()
</script>
-<h1>Changes for <span class="ns"><%= @example.function.ns %>/</span><%= @example.function.name %></h1>
+<h1>Changes for <span class="ns"><%= @example.function.namespace.name %>/</span><%= @example.function.name %></h1>
<% @versions.each do |v| %>
<% if v.user_id
@@ -22,15 +22,15 @@ xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
if r.instance_of? Comment
f = Function.find(r.commentable_id)
- title = "#{new_or_updated} comment on #{f.ns}/#{f.name} by #{author}."
+ title = "#{new_or_updated} comment on #{f.namespace.name}/#{f.name} by #{author}."
body = r.body
elsif r.instance_of? Example
f = r.function
- title = "#{new_or_updated} example for #{f.ns}/#{f.name} by #{author}."
+ title = "#{new_or_updated} example for #{f.namespace.name}/#{f.name} by #{author}."
body = r.body
elsif r.instance_of? SeeAlso
f = r.from_function
- title = "New see also for #{f.ns}/#{f.name} by #{author}."
+ title = "New see also for #{f.namespace.name}/#{f.name} by #{author}."
end
link = "http://clojuredocs.org#{f.href}"
Oops, something went wrong.

0 comments on commit 7ff50be

Please sign in to comment.