Skip to content

Commit

Permalink
updated views & controller. now we have a nice way of tagging project…
Browse files Browse the repository at this point in the history
…s at creation and update. it's similar to the way delicious handles tagging and is way nicer than before. :)
  • Loading branch information
bakkdoor committed Sep 7, 2008
1 parent 1f8bd3f commit f59838f
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 206 deletions.
76 changes: 2 additions & 74 deletions src/app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class ProjectsController < ApplicationController
auto_complete_for :tag, :name

before_filter :login_required, :only => [:new, :create, :edit, :update, :destroy]
before_filter :init_project_tags_session, :only => [:create, :update]

# GET /projects
# GET /projects.xml
Expand Down Expand Up @@ -40,9 +39,6 @@ def show
# GET /projects/new.xml
def new
@project = Project.new
@project.tag_list = session[:project_tags]

session[:project_tags] = []

respond_to do |format|
format.html # new.html.erb
Expand All @@ -53,31 +49,19 @@ def new
# GET /projects/1/edit
def edit
@project = Project.find(params[:id])

session[:project_tags] = []

# save names of project-tags in session
@project.tags.each do |t|
session[:project_tags] << t.name
end
end

# POST /projects
# POST /projects.xml
def create
@project = Project.new(params[:project])

# add tags, only if there are any specified
#@project.tag_list += session[:project_tags]
#params[:project][]

respond_to do |format|
if @project.save
@project_membership = ProjectMembership.new :user_id => current_user.id,
:project_id => @project.id,
:user_level => User.level_code(:project_admin)
if @project_membership.save
session[:project_tags] = nil # reset
flash[:notice] = (l :project_successful_create_notice)
format.html { redirect_to(@project) }
format.xml { render :xml => @project, :status => :created, :location => @project }
Expand All @@ -97,16 +81,8 @@ def create
def update
@project = Project.find(params[:id])

# add new tags, only if any specified
params[:new_tags].split(" ").each do |new_tag|
session[:project_tags] << new_tag
end

@project.tag_list = session[:project_tags]

respond_to do |format|
if @project.update_attributes(params[:project])
session[:project_tags] = nil # zurücksetzen
flash[:notice] = (l :project_successful_update_notice)
format.html { redirect_to(@project) }
format.xml { head :ok }
Expand Down Expand Up @@ -166,63 +142,19 @@ def auto_complete_for_tag_name
input_tags = params[:project][:tag_list].split(" ")
tag_name = input_tags.last
used_tags = input_tags - [input_tags.last]
session[:project_tags] = used_tags

#session[:project_tags] = used_tags
@tags = Tag.find(:all,
:conditions => [ 'LOWER(name) LIKE ?',
'%' + tag_name.downcase + '%' ],
:order => 'name ASC')

# only return tags, that aren't used yet...
@tags = @tags.select do |t|
not session[:project_tags].include? t.name
not used_tags.include? t.name
end
render :inline => "<%= auto_complete_result(@tags, 'name') %>"
end



def search_tags
if params[:search] != ""
@tags = Tag.search(params[:search])

if @tags.size > 0
# loop through project-tags-session (if exists)
# and mark all tags, which are already added to the project
# => only show remaining tags as searchresult, which haven't been added to project yet
# the used tags won't be shown in the view, by checking boolean value of :in_project in hash
# via css: style="display:none"

# a list of hashes, each having a tag and a boolean, if it's already used
@tag_used_pairs = []
if session[:project_tags]
@tags = @tags.each do |tag|
@tag_used_pairs << { :tag => tag, :in_project => session[:project_tags].include?(tag.name) }
end
end

render :partial => "add_tag_list", :object => @tag_used_pairs
else
render :text => "<br>Keine Tags gefunden."
end
else
render :text => ""
end
end

def add_tag
session[:project_tags] << params[:tag] unless session[:project_tags].include? params[:tag]

render :partial => "tag_list_item", :collection => session[:project_tags]
end

def remove_tag
session[:project_tags].delete params[:tag]

render :partial => "tag_list_item", :collection => session[:project_tags]
end


# shows all (public) projects of a given tag
def tag
@projects = Project.find_tagged_with( params[:id],
Expand All @@ -242,8 +174,4 @@ def search_projects

private

def init_project_tags_session
session[:project_tags] ||= []
end

end
14 changes: 0 additions & 14 deletions src/app/views/projects/_add_tag_list.rhtml

This file was deleted.

52 changes: 52 additions & 0 deletions src/app/views/projects/_edit_project.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<div class="headline"><%= title %></div>
<div class="form_new">
<% form_for(@project) do |f| %>
<%= f.error_messages %>
<table>
<tr>
<td><%= f.label :name, (l :name) %></td>
<td><%= f.text_field :name %></td>
</tr>
<tr>
<td><%= f.label :description, (l :description) %></td>
<td><%= f.text_area :description %></td>
</tr>
<tr>
<td><%= f.label :website, (l :website) %></td>
<td><%= f.text_field :website %></td>
</tr>
<tr>
<td><%= f.label :tag_name, (l :tags)%></td>
<td>
<%= f.text_field :tag_list, :size => 50 %>
<div class="auto_complete" id="tag_name_auto_complete"></div>
<script type="text/javascript">
var tag_name_auto_completer = new Ajax.Autocompleter('project_tag_list', 'tag_name_auto_complete',
'/projects/auto_complete_for_tag_name',
{
method:'get',
updateElement: function(item){
var tags = $('project_tag_list').value.split(" ");
tags.pop();
tags.push(item.innerText)
$('project_tag_list').value = tags.join(" ") + " "}}
)
</script>
</td>
</tr>
<tr>
<td><%= f.label :public, (l :public) %><br /></td>
<td><%= f.check_box :public %></td>
</tr>
<tr>
<td><%= f.label :invite_only, (l :invite_only) %><br /></td>
<td><%= f.check_box :invite_only %></td>
</tr>
<tr>
<td><%= f.submit (l :create) %></td>
</tr>
</table>
<% end %>
<%= link_to 'Back', projects_path %>
</div>
5 changes: 0 additions & 5 deletions src/app/views/projects/_tag_list_item.rhtml

This file was deleted.

61 changes: 1 addition & 60 deletions src/app/views/projects/edit.rhtml
Original file line number Diff line number Diff line change
@@ -1,60 +1 @@
<div class="headline">Editing project</div>
<div class="form_edit">
<% form_for(@project) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :name, (l :name) %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :description, (l :description) %><br />
<%= f.text_area :description %>
</p>
<p>
<%= f.label :tag_list, (l :tags) %>
<div id="project_tags" style="padding-bottom:1em">
<%= render :partial => "tag_list_item", :collection => @project.tag_list %>
</div>
<%= l :new_tags %>:
<%= text_field_tag :new_tags, {}, :size => 30,
:autocomplete => "off" %> (<%= l :seperated_by_comma %>)<br><br>
<%= l :tag_search %>:
<input id="tag_search" name="tag_search" type="text" value="" size="31" autocomplete="off">

<%= image_tag("spinner.gif",
:align => "absmiddle",
:border => 0,
:id => "spinner",
:style =>"display:none" ) %>
<div id="tag_search_results" style="text-align:left;padding-bottom:5em">
</div>
<%= observe_field 'tag_search',
:frequency => 0.5,
:before => "Element.show('spinner')",
:success => "Element.hide('spinner')",
:update => 'tag_search_results',
:url => { :controller => 'projects', :action=> 'search_tags' },
:with => "'search=' + escape(value)" %>

</p>
<p>
<%= f.label :website, (l :website) %><br />
<%= f.text_field :website %>
</p>
<p>
<%= f.label :public, (l :public) %><br />
<%= f.check_box :public %>
</p>
<p>
<%= f.label :invite_only, (l :invite_only) %><br />
<%= f.check_box :invite_only %>
</p>
<p>
<%= f.submit (l :update) %>
</p>
<% end %>
</div>

<%= link_to (l :show), @project %> |
<%= link_to (l :back), projects_path %>
<%= render :partial => "edit_project", :locals => { :title => (l :edit_project) }%>
54 changes: 1 addition & 53 deletions src/app/views/projects/new.rhtml
Original file line number Diff line number Diff line change
@@ -1,53 +1 @@
<div class="headline"><%= l :new_project %></div>
<div class="form_new">
<% form_for(@project) do |f| %>
<%= f.error_messages %>
<table>
<tr>
<td><%= f.label :name, (l :name) %></td>
<td><%= f.text_field :name %></td>
</tr>
<tr>
<td><%= f.label :description, (l :description) %></td>
<td><%= f.text_area :description %></td>
</tr>
<tr>
<td><%= f.label :website, (l :website) %></td>
<td><%= f.text_field :website %></td>
</tr>
<tr>
<td><%= f.label :tag_name, (l :tag_search)%></td>
<td>
<% text_field_with_auto_complete :tag, :name, {}, {:method => :get, :skip_style => true} %>
<input id="tag_list" name="project[tag_list]" size="30" type="text" />
<div class="auto_complete" id="tag_name_auto_complete"></div>
<script type="text/javascript">
var tag_name_auto_completer = new Ajax.Autocompleter('tag_list', 'tag_name_auto_complete',
'/projects/auto_complete_for_tag_name',
{
method:'get',
updateElement: function(item){
var tags = $('tag_list').value.split(" ");
tags.pop();
tags.push(item.innerText)
$('tag_list').value = tags.join(" ") + " "}}
)
</script>
</td>
</tr>
<tr>
<td><%= f.label :public, (l :public) %><br /></td>
<td><%= f.check_box :public %></td>
</tr>
<tr>
<td><%= f.label :invite_only, (l :invite_only) %><br /></td>
<td><%= f.check_box :invite_only %></td>
</tr>
<tr>
<td><%= f.submit (l :create) %></td>
</tr>
</table>
<% end %>
<%= link_to 'Back', projects_path %>
</div>
<%= render :partial => "edit_project", :locals => { :title => (l :new_project) }%>
1 change: 1 addition & 0 deletions src/lang/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ date: Datum
description: Beschreibung
destroy: Löschen
edit: Bearbeiten
edit_project: Projekt bearbeiten
email: Email
english: Englisch
failed_logins: Fehlgeschlagene Logins
Expand Down
1 change: 1 addition & 0 deletions src/lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ date: Datum
description: Description
destroy: Delete
edit: Edit
edit_project: Edit project
email: Email
english: English
failed_logins: Failed logins
Expand Down

0 comments on commit f59838f

Please sign in to comment.