Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added the ability to edit a watched repos description
  • Loading branch information
joelmoss committed Apr 11, 2012
1 parent 4818494 commit bb76739
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 47 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -14,6 +14,7 @@ gem 'sinatra'
gem 'coffeebeans'
gem 'exceptional'
gem 'scoped_search'
gem 'attribute_normalizer'

# Gems used only for assets and not required
# in production environments by default.
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -40,6 +40,7 @@ GEM
multi_json (~> 1.0)
addressable (2.2.7)
arel (3.0.2)
attribute_normalizer (1.1.0)
builder (3.0.0)
celluloid (0.10.0)
coffee-rails (3.2.2)
Expand Down Expand Up @@ -252,6 +253,7 @@ PLATFORMS
ruby

DEPENDENCIES
attribute_normalizer
coffee-rails (~> 3.2.1)
coffeebeans
debugger
Expand Down
60 changes: 60 additions & 0 deletions app/assets/javascripts/application.js.coffee
Expand Up @@ -21,6 +21,66 @@ $ ->
$("a[rel=tooltip]").tooltip()


$ ->
$('a.edit-description').on 'click', ->
btn = $(this)
repo = btn.parents 'tr.repo'
ele = repo.find 'p.description'

if btn.hasClass 'btn-mini'
ele.hide()

cancel = btn.clone()
cancel.text 'cancel'
cancel.addClass 'cancel-edit'
cancel.removeClass 'btn-danger'
cancel.insertAfter btn

btn.removeClass 'btn-mini'
btn.text 'save'

ele.after '<form><textarea name="watch[description]"></textarea></form>'
repo.find('textarea').val ele.text()

else
form = repo.find 'form'

$.ajax
type: 'PUT'
data: form.serializeArray()
url: "repos/#{repo.attr('id').replace('repo-', '')}"

ele.text(repo.find('textarea').val()).show()
repo.find('.cancel-edit').remove()
form.remove()

btn.addClass 'btn-mini'
btn.text 'edit...'


$('.repo').on 'click', '.cancel-edit', ->
btn = $(this)
repo = btn.parents 'tr.repo'
ele = repo.find 'p.description'
edit = repo.find '.edit-description'
form = repo.find 'form'

ele.show()
btn.remove()
form.remove()

edit.addClass 'btn-mini'
edit.text 'edit...'
edit.hide()


$('tr.repo').on
mouseenter: ->
$(this).find('a.edit-description').css 'display', 'block'
mouseleave: ->
$(this).find('a.edit-description').hide()


# Fetch repos on loading page
# -----------------------------------------------------------------------------

Expand Down
24 changes: 24 additions & 0 deletions app/assets/stylesheets/app/styles.css.scss
@@ -1,6 +1,11 @@
/* Utilities
/* ---------------------------------------------------------------------------- */

td.center {
text-align: center;
}


.table th.right, .table td.right, .right {
text-align: right;
}
Expand Down Expand Up @@ -36,4 +41,23 @@ tr.unwatched {
color: #DDD;
text-decoration: line-through;
}
}

tr.repo {
.edit-description {
display: none;
margin-top: 4px;
}

form {
margin: 0;
textarea {
height: 50px;
width: 96%;
}
}

.cancel-edit {
margin-top: 3px;
}
}
2 changes: 1 addition & 1 deletion app/controllers/languages_controller.rb
Expand Up @@ -11,7 +11,7 @@ def index
def show
language = params[:id] == 'other' ? nil : params[:id]
params[:order] ||= 'repos.watchers_count DESC'
@repos = current_user.watchings.where(language: language).page(params[:page]).includes(:owner).order(params[:order])
@repos = current_user.watches.where('repos.language' => language).page(params[:page]).includes(watching: :owner).order(params[:order])
end

end
10 changes: 9 additions & 1 deletion app/controllers/repos_controller.rb
Expand Up @@ -11,7 +11,7 @@ class ReposController < ApplicationController
def index
respond_to do |wants|
wants.html do
@repos = current_user.watchings.includes(:owner).page(params[:page]).order(params[:order] ||= 'repos.watchers_count DESC')
@repos = current_user.watches.includes(watching: :owner).page(params[:page]).order(params[:order] ||= 'repos.watchers_count DESC')
end
wants.js do
render json: { success: current_user.watchings.count > 0 }
Expand All @@ -27,6 +27,14 @@ def unwatch
current_user.watches.unwatch @repo
end

def update
@repo = current_user.watches.where(repo_id: params[:id]).first
raise ActiveRecord::RecordNotFound if @repo.blank?

@repo.update_attributes params[:watch]
render nothing: true, layout: false
end


private

Expand Down
4 changes: 3 additions & 1 deletion app/controllers/searches_controller.rb
Expand Up @@ -7,11 +7,13 @@ def index
params[:per_page] ||= 20
params[:page] ||= 1
order = params[:order] ||= 'watchers_count DESC'
query = params[:q]

search = Tire::Search::Search.new(GITCHEN_INDEX_NAME, :type => :watch, :query => params[:q])
search = Tire::Search::Search.new(GITCHEN_INDEX_NAME, :type => :watch)
search.size params[:per_page]
search.from params[:page].to_i <= 1 ? 0 : (params[:per_page].to_i * (params[:page].to_i-1))
search.filter :terms, watcher_id: [ current_user.id ]
search.query { string query }
search.sort do
field_name, direction = order.split(' ')
by field_name.to_sym, direction.downcase
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Expand Up @@ -10,8 +10,8 @@ def index
end

def show
@repos = current_user.watchings.where('users.username' => params[:id]).page(params[:page]).
includes(:owner).order(params[:order] ||= 'repos.watchers_count DESC')
@repos = current_user.watches.where('users.username' => params[:id]).page(params[:page]).
includes(watching: :owner).order(params[:order] ||= 'repos.watchers_count DESC')
end

end
9 changes: 9 additions & 0 deletions app/models/watch.rb
Expand Up @@ -8,6 +8,7 @@ class Watch < ActiveRecord::Base
indexes :name, as: 'watching.name.gsub(/_/, " ")'
indexes :original_name, as: 'watching.name', index: 'not_analyzed'
indexes :description, as: 'watching.description'
indexes :watch_description, as: 'description'
indexes :language, as: 'watching.language'
indexes :private, as: 'watching.private', index: 'not_analyzed'
indexes :watchers_count, as: 'watching.watchers_count', index: 'not_analyzed', type: 'integer'
Expand All @@ -23,6 +24,14 @@ class Watch < ActiveRecord::Base

default_scope where(deleting: false)

normalize_attributes :description

attr_accessible :description


def description
read_attribute(:description) || watching.description
end

# Unwatch a repo (duh!).
def unwatch
Expand Down
2 changes: 1 addition & 1 deletion app/views/languages/show.html.erb
Expand Up @@ -16,7 +16,7 @@

<table class="table table-striped">
<tbody>
<%= render @repos %>
<%= render partial: 'repos/repo', collection: @repos %>
</tbody>
</table>

Expand Down
30 changes: 16 additions & 14 deletions app/views/repos/_repo.html.erb
@@ -1,28 +1,30 @@
<tr id="repo-<%= repo.id %>">
<td class="line-height6"><nobr>
<%= link_to "https://github.com/#{repo.owner}/#{repo}/watchers", target: '_blank' do %>
<tr class="repo" id="repo-<%= repo.watching.id %>">
<td class="line-height5 center"><nobr>
<%= link_to "https://github.com/#{repo.watching.owner}/#{repo.watching}/watchers", target: '_blank' do %>
<span class="badge badge-success">
<i class="icon-eye-<%= repo.private? ? 'close' : 'open' %> icon-white"></i>
<%= repo.watchers.size %>
<i class="icon-eye-<%= repo.watching.private? ? 'close' : 'open' %> icon-white"></i>
<%= repo.watching.watchers.size %>
</span>
<% end -%>
<%= link_to 'edit...', '#', class: 'edit-description btn btn-danger btn-mini' %>
</nobr></td>
<td>
<td style="width:100%;">
<h3>
<%= link_to repo.owner, user_path(repo.owner.username) %> /
<%= link_to repo_path(repo.owner.username, repo.name), class: 'external-link', data: { target: '#repo-modal', toggle: "modal-ajax" } do %>
<%= repo %>
<%= link_to repo.watching.owner, user_path(repo.watching.owner.username) %> /
<%= link_to repo_path(repo.watching.owner.username, repo.watching.name), class: 'external-link', data: { target: '#repo-modal', toggle: "modal-ajax" } do %>
<%= repo.watching %>
<%- end -%>
<a href="<%= "https://github.com/#{repo.owner}/#{repo}" %>" target="_blank"><i class="icon-share-alt"></i></a>
<a href="<%= "https://github.com/#{repo.watching.owner}/#{repo.watching}" %>" target="_blank"><i class="icon-share-alt"></i></a>
</h3>
<p class="muted"><%= repo.description %></p>
<p class="description muted"><%= repo.description %></p>
</td>
<td class="muted"><h6 class="line-height6"><%= repo.language %></h6></td>
<td class="muted"><h6 class="line-height6"><%= repo.watching.language %></h6></td>
<td class="line-height6"><nobr>
<%= link_to "https://github.com/#{repo.owner}/#{repo}/network", target: '_blank' do %>
<%= link_to "https://github.com/#{repo.watching.owner}/#{repo.watching}/network", target: '_blank' do %>
<span class="badge">
<i class="icon-random icon-white"></i>
<%= repo.forks %>
<%= repo.watching.forks %>
</span>
<% end -%>
</nobr></td>
Expand Down
2 changes: 1 addition & 1 deletion app/views/repos/index.html.erb
Expand Up @@ -26,7 +26,7 @@

<table class="table table-striped">
<tbody>
<%= render @repos %>
<%= render partial: 'repos/repo', collection: @repos %>
</tbody>
</table>

Expand Down
4 changes: 2 additions & 2 deletions app/views/repos/show.html.erb
Expand Up @@ -26,8 +26,8 @@
</span>
<% end -%>
<%= link_to language_path(@repo.language) do %>
<span class="badge badge-info pull-left margin-left margin-top"><%= @repo.language %></span>
<%= link_to language_path(@repo.language || 'other') do %>
<span class="badge badge-info pull-left margin-left margin-top"><%= @repo.language || 'Unknown' %></span>
<% end -%>

<div class="btn-group pull-right">
Expand Down
8 changes: 5 additions & 3 deletions app/views/searches/_repo.html.erb
@@ -1,21 +1,23 @@
<tr id="repo-<%= repo.id %>">
<tr class="repo" id="repo-<%= repo.id %>">
<td class="line-height6"><nobr>
<%= link_to "https://github.com/#{repo.owner}/#{repo.original_name}/watchers", target: '_blank' do %>
<span class="badge badge-success">
<i class="icon-eye-<%= repo.private ? 'close' : 'open' %> icon-white"></i>
<%= repo.watchers_count %>
</span>
<% end -%>
<%= link_to 'edit...', '#', class: 'edit-description btn btn-danger btn-mini' %>
</nobr></td>
<td>
<td style="width:100%;">
<h3>
<%= link_to repo.owner, user_path(repo.owner) %> /
<%= link_to repo_path(repo.owner, repo.original_name), class: 'external-link', data: { target: '#repo-modal', toggle: "modal-ajax" } do %>
<%= repo.original_name %>
<%- end -%>
<a href="<%= "https://github.com/#{repo.owner}/#{repo.original_name}" %>" target="_blank"><i class="icon-share-alt"></i></a>
</h3>
<p class="muted"><%= repo.description %></p>
<p class="description muted"><%= repo.watch_description %></p>
</td>
<td class="muted"><h6 class="line-height6"><%= repo.language %></h6></td>
<td class="line-height6"><nobr>
Expand Down
30 changes: 16 additions & 14 deletions app/views/users/_repo.html.erb
@@ -1,28 +1,30 @@
<tr id="repo-<%= repo.id %>">
<td class="line-height6"><nobr>
<%= link_to "https://github.com/#{repo.owner}/#{repo}/watchers", target: '_blank' do %>
<tr class="repo" id="repo-<%= repo.watching.id %>">
<td class="line-height5 center"><nobr>
<%= link_to "https://github.com/#{repo.watching.owner}/#{repo.watching}/watchers", target: '_blank' do %>
<span class="badge badge-success">
<i class="icon-eye-<%= repo.private? ? 'close' : 'open' %> icon-white"></i>
<%= repo.watchers.size %>
<i class="icon-eye-<%= repo.watching.private? ? 'close' : 'open' %> icon-white"></i>
<%= repo.watching.watchers.size %>
</span>
<% end -%>
<%= link_to 'edit...', '#', class: 'edit-description btn btn-danger btn-mini' %>
</nobr></td>
<td>
<td style="width:100%;">
<h3>
<small><strong><%= repo.owner %></strong> /</small>
<%= link_to repo_path(repo.owner.username, repo.name), class: 'external-link', data: { target: '#repo-modal', toggle: "modal-ajax" } do %>
<%= repo %>
<small><strong><%= repo.watching.owner %></strong> /</small>
<%= link_to repo_path(repo.watching.owner.username, repo.watching.name), class: 'external-link', data: { target: '#repo-modal', toggle: "modal-ajax" } do %>
<%= repo.watching %>
<%- end -%>
<a href="<%= "https://github.com/#{repo.owner}/#{repo}" %>" target="_blank"><i class="icon-share-alt"></i></a>
<a href="<%= "https://github.com/#{repo.watching.owner}/#{repo.watching}" %>" target="_blank"><i class="icon-share-alt"></i></a>
</h3>
<p class="muted"><%= repo.description %></p>
<p class="description muted"><%= repo.description %></p>
</td>
<td class="muted"><h6 class="line-height6"><%= repo.language %></h6></td>
<td class="muted"><h6 class="line-height6"><%= repo.watching.language %></h6></td>
<td class="line-height6"><nobr>
<%= link_to "https://github.com/#{repo.owner}/#{repo}/network", target: '_blank' do %>
<%= link_to "https://github.com/#{repo.watching.owner}/#{repo.watching}/network", target: '_blank' do %>
<span class="badge">
<i class="icon-random icon-white"></i>
<%= repo.forks %>
<%= repo.watching.forks %>
</span>
<% end -%>
</nobr></td>
Expand Down
8 changes: 5 additions & 3 deletions config/routes.rb
Expand Up @@ -5,9 +5,11 @@
get 'sign_in', to: 'sessions#new', as: :sign_in
delete 'sign_out', to: 'sessions#destroy', as: :sign_out

resources :repos, only: [ :index ]
get 'repos/:owner/:id' => 'repos#show', as: :repo
delete 'repos/:owner/:id' => 'repos#unwatch', as: :unwatch_repo
resources :repos, only: [ :index, :update ]
scope :constraints => { :id => /.*/ } do
get 'repos/:owner/:id' => 'repos#show', as: :repo
delete 'repos/:owner/:id' => 'repos#unwatch', as: :unwatch_repo
end

resources :languages, :users, :searches, only: [ :index, :show ]

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20120411191040_add_description_to_watches.rb
@@ -0,0 +1,5 @@
class AddDescriptionToWatches < ActiveRecord::Migration
def change
add_column :watches, :description, :string
end
end

0 comments on commit bb76739

Please sign in to comment.