Skip to content

Commit

Permalink
Added filters
Browse files Browse the repository at this point in the history
  • Loading branch information
ulitink committed Oct 31, 2011
1 parent 336752a commit f7639a9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 24 deletions.
28 changes: 22 additions & 6 deletions app/controllers/feedbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
class FeedbacksController < ApplicationController
def list
feeds = Feed.all
#feeds.each { |feed| feed.update_content }

@records = Record.paginate(:page => params[:page]).order('posted_at DESC')
set_fields
end

def update_posts
Expand All @@ -25,7 +22,7 @@ def update_posts
render :update do |page|
removed_records.each { |element_id| page.remove element_id }
added_records.each do |record|
page.insert_html(:top, 'feedback_list', render(:partial => 'record', :locals => { :record => record }))
page.insert_html(:top, 'feedback_list', :partial => 'record', :locals => { :record => record })
end
end
end
Expand All @@ -36,7 +33,26 @@ def update_status
record.user = current_user
record.save
render :update do |page|
page.replace("record#{record.id}", render(:partial => 'record', :locals => { :record => record }))
page.replace("record#{record.id}", :partial => 'record', :locals => { :record => record })
end
end

def update_filter
set_fields
render :update do |page|
page.replace('content', :template => 'feedbacks/list')
end
end

private

def set_fields
if !params[:filter].nil? && params[:filter][:only_watched]=='true'
@records = current_user.watched_records(params[:page])
@filters = {:only_watched => true}
else
@records = Record.paginate(:page => params[:page]).order('posted_at DESC')
@filters = {:only_watched => false}
end
end

Expand Down
14 changes: 11 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me

has_and_belongs_to_many :feeds
Expand All @@ -13,4 +10,15 @@ def full_name
#TODO add name and surname
email.rpartition('@')[0]
end

def watched_records(page)
Record.paginate(:page => page).find(
:all,
:joins => 'JOIN feeds child_feeds ON records.feed_id=child_feeds.id' <<
' LEFT OUTER JOIN feeds parent_feeds ON parent_feeds.id=child_feeds.parent_id' <<
' JOIN feeds_users ON feeds_users.feed_id=parent_feeds.id OR feeds_users.feed_id=child_feeds.id',
:conditions => ['feeds_users.user_id = ?', id],
:order => 'posted_at DESC'
)
end
end
13 changes: 8 additions & 5 deletions app/views/feedbacks/list.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="container">
<div id="content">
<div id="feedbacks">
<h2>Recent</h2>
<%= will_paginate @records %>
Expand All @@ -9,13 +9,16 @@
</div>
<div id="filters">
<h2>Filters</h2>
By tag, only unread etc.
<br />
TODO
<%= form_for :filter, {:url => {:action => 'update_filter'}, :remote => true}, :html => {:id => 'filters_form'} do |f| %>
<% only_watched = @filters.nil? ? false : @filters[:only_watched] %>
<%= f.radio_button :only_watched, false, :checked => !only_watched, :disabled => !user_signed_in?, :onchange => "$('filters_form').submit()" %>
<%= f.label :only_watched, 'All' %><br />
<%= f.radio_button :only_watched, true, :checked => only_watched, :disabled => !user_signed_in?, :onchange => "$('filters_form').submit()" %>
<%= f.label :only_watched, 'Only my' %><br />
<% end %>
</div>
<script type="text/javascript">
document.observe("dom:loaded", function() {
// initially hide all containers for tab content
<%= remote_function :url => {:action => 'update_posts'}, :with => 'collect_record_ids()' %>
});
</script>
Expand Down
4 changes: 2 additions & 2 deletions app/views/profiles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<h1>Edit profile</h1>
<h2>Add feed to watched</h2>
<%= form_for :feed, {:url => {:action => 'new_watched_feed'}, :remote => true} do |f| %>
Type:<%= f.text_field :loader_arg %><br />
Loader argument:<%= select :feed, :loader, Feed::STRATEGY_BY_LOADER.keys %><br />
Type:<%= select :feed, :loader, Feed::STRATEGY_BY_LOADER.keys %><br />
Loader argument:<%= f.text_field :loader_arg %><br />
<%= f.submit 'Add' %>
<% end %>
<div style="margin: 10px 0px;">
Expand Down
11 changes: 6 additions & 5 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ development:
# Do not set this db to the same as development or production.
test:
adapter: postgresql
encoding: unicode
database: FeedbackAggr_test
pool: 5
username: FeedbackAggr
password:
encoding: utf8
host: localhost
port: 5432
database: feedback_aggr_test
username: postgres
password: 12qw

production:
adapter: postgresql
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

match '/feedbacks/update_status'
match '/feedbacks/update_posts'
match '/feedbacks/update_filter'
match '/profiles/edit'
match '/profiles/new_watched_feed'
match '/profiles/remove_watched_feed'
Expand Down
4 changes: 1 addition & 3 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#

two: {}
# column: value

0 comments on commit f7639a9

Please sign in to comment.