Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implemented 'pin'. [#40 state:resolved]

  • Loading branch information...
commit 45e9b767586125c6b77a8decfd102d74e15ccd58 1 parent 8d0d6d9
Hiroshi Nakamura authored
16 app/controllers/application.rb
View
@@ -118,7 +118,7 @@ def set_user(user)
end
def logout
- update_checked_modified(session[:checked])
+ update_checked_modified
@user_id = session[:user_id] = nil
@auth = nil
reset_session
@@ -135,9 +135,21 @@ def v(hash, *keywords)
}
end
- def update_checked_modified(store)
+ def update_checked_modified
+ store = session[:checked]
if @auth and store
EntryThread.update_checked_modified(@auth, store)
+ session[:checked] = {}
+ end
+ end
+
+ def clear_checked_modified(eid)
+ cond = ['user_id = ? and last_modifieds.eid = ?', @auth.id, eid]
+ if checked = CheckedModified.find(:first, :conditions => cond, :include => 'last_modified')
+ checked.destroy
+ end
+ if checked = session[:checked]
+ checked.delete(eid)
end
end
end
36 app/controllers/entry_controller.rb
View
@@ -217,11 +217,10 @@ def updated
ctx.updated = true
ctx.fold = param(:fold) != 'no'
}
- store = session[:checked] ||= {}
if param(:submit) == 'refresh' or updated_expired(Time.now)
- update_checked_modified(store)
- store = session[:checked] = {}
+ update_checked_modified
end
+ store = session[:checked] ||= {}
@entries = EntryThread.find(@ctx.find_opt) || []
@entries.each do |t|
t.entries.each do |e|
@@ -425,6 +424,37 @@ def unlike
redirect_to_list
end
+ verify :only => :pin,
+ :method => :get,
+ :params => [:id],
+ :add_flash => {:error => 'verify failed'},
+ :redirect_to => {:action => 'list'}
+
+ def pin
+ id = param(:id)
+ if id
+ Entry.add_pin(create_opt(:id => id))
+ clear_checked_modified(id)
+ end
+ flash[:keep_ctx] = true
+ redirect_to_list
+ end
+
+ verify :only => :unpin,
+ :method => :get,
+ :params => [:id],
+ :add_flash => {:error => 'verify failed'},
+ :redirect_to => {:action => 'list'}
+
+ def unpin
+ id = param(:id)
+ if id
+ Entry.delete_pin(create_opt(:id => id))
+ end
+ flash[:keep_ctx] = true
+ redirect_to_list
+ end
+
private
def capture_title(url)
2  app/helpers/application_helper.rb
View
@@ -29,6 +29,8 @@ module ApplicationHelper
'related' => 'link.png',
'go' => 'page_white_world.png',
'media_disabled' => 'image_link.png',
+ 'pinned' => 'tick.png',
+ 'pin' => 'anchor.png',
}
def icon_url(name)
10 app/helpers/entry_helper.rb
View
@@ -9,6 +9,14 @@ def ctx
@ctx
end
+ def pin_link(entry)
+ if entry.pinned?
+ link_to(icon_tag(:pinned, 'unpin'), :action => 'unpin', :id => entry.id)
+ else
+ link_to(icon_tag(:pin), :action => 'pin', :id => entry.id)
+ end
+ end
+
def icon(entry)
service_id = entry.service_id
name = v(entry, 'service', 'name')
@@ -285,7 +293,7 @@ def likes(entry, compact)
likes = me + rest
if !likes.empty?
if liked?(entry)
- icon = link_to(icon_tag(:star, 'un-like'), :action => 'unlike', :id => u(entry.id))
+ icon = link_to(icon_tag(:star, 'unlike'), :action => 'unlike', :id => u(entry.id))
else
icon = icon_tag(:star)
end
23 app/models/entry.rb
View
@@ -53,6 +53,25 @@ def delete_like(opt)
ff_client.unlike(auth.name, auth.remote_key, id)
end
+ def add_pin(opt)
+ auth = opt[:auth]
+ id = opt[:id]
+ unless Pin.find_by_user_id_and_eid(auth.id, id)
+ pin = Pin.new
+ pin.user = auth
+ pin.eid = id
+ raise unless pin.save
+ end
+ end
+
+ def delete_pin(opt)
+ auth = opt[:auth]
+ id = opt[:id]
+ if pin = Pin.find_by_user_id_and_eid(auth.id, id)
+ raise unless pin.destroy
+ end
+ end
+
private
def ff_client
@@ -60,6 +79,10 @@ def ff_client
end
end
+ def pinned?
+ !!v(EntryThread::MODEL_PIN_TAG)
+ end
+
def similar?(rhs)
result = false
if self.user_id == rhs.user_id
23 app/models/entry_thread.rb
View
@@ -1,5 +1,6 @@
class EntryThread
MODEL_LAST_MODIFIED_TAG = '__model_last_modified'
+ MODEL_PIN_TAG = '__model_pin'
class << self
def find(opt = {})
@@ -30,10 +31,14 @@ def find(opt = {})
if opt[:updated]
wrapped = filter_checked_entries(auth, wrapped)
end
+ if opt[:updated] or opt[:id]
+ wrapped = check_pin(auth, wrapped)
+ end
sort_by_service(wrapped, opt)
end
def update_checked_modified(auth, hash)
+ pinned = pinned_map(auth, hash.keys)
cond = [
'user_id = ? and last_modifieds.eid in (?)',
auth.id,
@@ -42,6 +47,7 @@ def update_checked_modified(auth, hash)
checked = CheckedModified.find(:all, :conditions => cond, :include => 'last_modified')
hash.each do |eid, checked_modified|
next unless checked_modified
+ next if pinned.key?(eid)
if c = checked.find { |e| e.last_modified.eid == eid }
c.checked = Time.parse(checked_modified)
raise unless c.save
@@ -96,6 +102,23 @@ def filter_checked_entries(auth, entries)
}
end
+ def check_pin(auth, entries)
+ pinned = pinned_map(auth, entries.map { |e| e.id })
+ entries.each do |entry|
+ entry[MODEL_PIN_TAG] = true if pinned.key?(entry.id)
+ end
+ entries
+ end
+
+ def pinned_map(auth, eids)
+ cond = [
+ 'user_id = ? and eid in (?)',
+ auth.id,
+ eids
+ ]
+ Pin.find(:all, :conditions => cond).inject({}) { |r, e| r[e.eid] = true; r }
+ end
+
def search_entries(auth, opt)
query = opt[:query]
search = filter_opt(opt)
3  app/models/pin.rb
View
@@ -0,0 +1,3 @@
+class Pin < ActiveRecord::Base
+ belongs_to :user
+end
1  app/views/entry/_entry.html.erb
View
@@ -1,6 +1,7 @@
<%- if entry.respond_to?(:fold_entries) -%>
<%= fold_link(entry) %>
<%- else -%>
+ <%= pin_link(entry) if ctx.updated or ctx.single? -%>
<%= icon(entry) %>
<%= published(entry, false) if ctx.single? -%>
<%= author_link(entry, (root_user.nil? or root_user != entry.user_id), (root_service.nil? or root_service != entry.service_identity)) %>
20 db/migrate/20090210144137_insert_profile_of_user.rb
View
@@ -1,20 +1,24 @@
class InsertProfileOfUser < ActiveRecord::Migration
def self.up
User.find(:all).each do |user|
- if user.profile.nil?
- user.profile = Profile.new
- user.profile.font_size = F2P::Config.font_size
- user.profile.entries_in_page = F2P::Config.entries_in_page
- user.profile.text_folding_size = F2P::Config.text_folding_size
- raise unless user.profile.save
+ if defined?(Profile)
+ if user.profile.nil?
+ user.profile = Profile.new
+ user.profile.font_size = F2P::Config.font_size
+ user.profile.entries_in_page = F2P::Config.entries_in_page
+ user.profile.text_folding_size = F2P::Config.text_folding_size
+ raise unless user.profile.save
+ end
end
end
end
def self.down
User.find(:all).each do |user|
- user.profile = nil
- raise unless user.save
+ if defined?(Profile)
+ user.profile = nil
+ raise unless user.save
+ end
end
end
end
8 db/migrate/20090212121807_add_entries_in_thread_to_profile.rb
View
@@ -1,9 +1,11 @@
class AddEntriesInThreadToProfile < ActiveRecord::Migration
def self.up
add_column :profiles, :entries_in_thread, :integer
- Profile.find(:all).each do |profile|
- profile.entries_in_thread = F2P::Config.entries_in_thread
- profile.save
+ if defined?(Profile)
+ Profile.find(:all).each do |profile|
+ profile.entries_in_thread = F2P::Config.entries_in_thread
+ profile.save
+ end
end
end
13 db/migrate/20090302110246_create_pins.rb
View
@@ -0,0 +1,13 @@
+class CreatePins < ActiveRecord::Migration
+ def self.up
+ create_table :pins do |t|
+ t.integer 'user_id', :null => false
+ t.string 'eid', :null => false
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :pins
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.