Skip to content

Commit

Permalink
Merge pull request #210 from onk/feature/replace_dynamic_finder
Browse files Browse the repository at this point in the history
replace find_by_xxx to find_by
  • Loading branch information
makimoto committed Mar 3, 2015
2 parents b43b2de + 1340268 commit 921cb4a
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion app/controllers/about_controller.rb
@@ -1,7 +1,7 @@
class AboutController < ApplicationController
def index
url = url_from_path(:url) unless params[:url].blank?
@feed = Feed.find_by_feedlink(url) unless url.blank?
@feed = Feed.find_by(feedlink: url) unless url.blank?
unless @feed.nil?
@is_feedlink = true
respond_to do |format|
Expand Down
24 changes: 12 additions & 12 deletions app/controllers/api/feed_controller.rb
Expand Up @@ -13,14 +13,14 @@ def discover
url = URI.parse(params[:url])
FeedSearcher.search(url.to_s).each do |feedlink|
feedlink = (url + feedlink).to_s
if feed = Feed.find_by_feedlink(feedlink)
if feed = Feed.find_by(feedlink: feedlink)
result = {
subscribers_count: feed.subscribers_count,
feedlink: feed.feedlink,
link: feed.link,
title: feed.title,
}
if sub = @member.subscriptions.find_by_feed_id(feed.id)
if sub = @member.subscriptions.find_by(feed_id: feed.id)
result[:subscribe_id] = sub.id
end
feeds << result
Expand Down Expand Up @@ -80,12 +80,12 @@ def subscribed
sub_id = (params[:subscribe_id] || 0).to_i
sub = nil
if sub_id > 0
sub = @member.subscriptions.find_by_id(sub_id)
sub = @member.subscriptions.find_by(id: sub_id)
else
if feedlink.blank? or (feed = Feed.find_by_feedlink(feedlink)).nil?
if feedlink.blank? or (feed = Feed.find_by(feedlink: feedlink)).nil?
return render_json_status(false)
end
sub = @member.subscriptions.find_by_feed_id(feed.id)
sub = @member.subscriptions.find_by(feed_id: feed.id)
end
unless sub
return render_json_status(false)
Expand Down Expand Up @@ -132,15 +132,15 @@ def move
if dest.blank?
folder_id = nil
else
unless folder = @member.folders.find_by_name(dest)
unless folder = @member.folders.find_by_id(dest.to_i)
unless folder = @member.folders.find_by(name: dest)
unless folder = @member.folders.find_by(id: dest.to_i)
return render_json_status(false)
end
end
folder_id = folder.id
end
(params[:subscribe_id] || "").split(/\s*,\s*/).each do |id|
if sub = @member.subscriptions.find_by_id(id)
if sub = @member.subscriptions.find_by(id: id)
sub.update_attribute(:folder_id, folder_id)
end
end
Expand All @@ -162,7 +162,7 @@ def set_notify
end
ignore = ignore.to_i != 0
sub_id.split(/\s*,\s*/).each do |id|
if sub = @member.subscriptions.find_by_id(id.to_i)
if sub = @member.subscriptions.find_by(id: id.to_i)
sub.update_attribute(:ignore_notify, ignore)
end
end
Expand All @@ -176,7 +176,7 @@ def set_public
end
is_public = is_public.to_i != 0
sub_id.split(/\s*,\s*/).each do |id|
if sub = @member.subscriptions.find_by_id(id.to_i)
if sub = @member.subscriptions.find_by(id: id.to_i)
sub.update_attribute(:public, is_public)
end
end
Expand All @@ -191,7 +191,7 @@ def remove_tags

def fetch_favicon
feedlink = params[:feedlink]
if feedlink.blank? or (feed = Feed.find_by_feedlink(feedlink)).nil?
if feedlink.blank? or (feed = Feed.find_by(feedlink: feedlink)).nil?
return render_json_status(false)
end
feed.fetch_favicon!
Expand All @@ -208,6 +208,6 @@ def get_subscription
if params[:subscribe_id].blank? or (sub_id = params[:subscribe_id].to_i) <= 0
return nil
end
@member.subscriptions.find_by_id(sub_id)
@member.subscriptions.find_by(id: sub_id)
end
end
4 changes: 2 additions & 2 deletions app/controllers/api/folder_controller.rb
Expand Up @@ -9,7 +9,7 @@ class Api::FolderController < ApplicationController
def create
name = params[:name]
Folder.transaction do
if @member.folders.find_by_name(name)
if @member.folders.find_by(name: name)
return render_json_status(false, ERR_ALREADY_EXISTS)
end
@member.folders.create(name: name)
Expand Down Expand Up @@ -39,7 +39,7 @@ def update

def get_folder
if (folder_id = params[:folder_id].to_i) > 0
return @member.folders.find_by_id(folder_id)
return @member.folders.find_by(id: folder_id)
end
return nil
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/pin_controller.rb
Expand Up @@ -20,7 +20,7 @@ def add
end

def remove
unless pin = current_member.pins.find_by_link(params[:link])
unless pin = current_member.pins.find_by(link: params[:link])
return render_json_status(false, ErrorCode::NOT_FOUND)
end
pin.destroy
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api_controller.rb
Expand Up @@ -42,7 +42,7 @@ def unread

def touch_all
params[:subscribe_id].split(/\s*,\s*/).each do |id|
if sub = @member.subscriptions.find_by_id(id)
if sub = @member.subscriptions.find_by(id: id)
sub.update_attributes(has_unread: false, viewed_on: DateTime.now)
end
end
Expand Down Expand Up @@ -163,7 +163,7 @@ def crawl

def find_sub
@id = (params[:subscribe_id] || params[:id] || 0).to_i
unless @sub = @member.subscriptions.includes(:feed).find_by_id(@id)
unless @sub = @member.subscriptions.includes(:feed).find_by(id: @id)
render NOTHING
return false
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/favicon_controller.rb
Expand Up @@ -2,7 +2,7 @@ class FaviconController < ApplicationController
def get
image = nil
feed_id = url_from_path(:feed)
feed = feed_id.match(/^\d+$/) ? Feed.find_by_id(feed_id.to_i) : Feed.find_by_feedlink(feed_id)
feed = feed_id.match(/^\d+$/) ? Feed.find_by(id: feed_id.to_i) : Feed.find_by(feedlink: feed_id)
if feed and feed.favicon
image = feed.favicon.image
else
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/import_controller.rb
Expand Up @@ -23,7 +23,7 @@ def fetch
@folders[folder_name] += outlines.select {|outline| outline.attributes["xml_url"].present? }.map do |outline|
attributes = outline.attributes.dup
feedlink = attributes["xml_url"]
feed = Feed.find_by_feedlink(feedlink)
feed = Feed.find_by(feedlink: feedlink)
item = {}
item[:title] = attributes["title"] || attributes["text"] || feedlink
item[:link] = attributes["url"] || attributes["html_url"] || feedlink
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/subscribe_controller.rb
Expand Up @@ -12,7 +12,7 @@ def confirm
# params[:url] is http:/example.com because of squeeze("/")
@url = url_from_path(:url)
FeedSearcher.search(@url).each do |feedlink|
if feed = Feed.find_by_feedlink(feedlink)
if feed = Feed.find_by(feedlink: feedlink)
if sub = current_member.subscribed(feed)
feed[:subscribe_id] = sub.id
end
Expand Down
10 changes: 5 additions & 5 deletions app/models/member.rb
Expand Up @@ -39,7 +39,7 @@ class Member < ActiveRecord::Base

# Authenticates a user by their login name and unencrypted password. Returns the user or nil.
def self.authenticate(username, password)
u = find_by_username(username) # need to get the salt
u = find_by(username: username) # need to get the salt
u && u.authenticated?(password) ? u : nil
end

Expand Down Expand Up @@ -95,7 +95,7 @@ def subscribe_feed(feedlink, options = {})
return nil
end

unless feed = Feed.find_by_feedlink(feedlink)
unless feed = Feed.find_by(feedlink: feedlink)
if options[:quick]
feed = Feed.create({
feedlink: feedlink,
Expand All @@ -112,7 +112,7 @@ def subscribe_feed(feedlink, options = {})
options.delete(:quick)
options.delete(:title)

if sub = self.subscriptions.find_by_feed_id(feed.id)
if sub = self.subscriptions.find_by(feed_id: feed.id)
return sub
end

Expand All @@ -124,11 +124,11 @@ def subscribe_feed(feedlink, options = {})
end

def subscribed(feed)
self.subscriptions.find_by_feed_id(feed.id)
self.subscriptions.find_by(feed_id: feed.id)
end

def check_subscribed(feedlink)
if feed = Feed.find_by_feedlink(feedlink)
if feed = Feed.find_by(feedlink: feedlink)
return self.subscribed(feed)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fastladder/crawler.rb
Expand Up @@ -190,7 +190,7 @@ def delete_old_items_if_new_items_are_many(new_items_size)

def update_or_insert_items_to_feed(feed, items, result)
items.reverse_each do |item|
if old_item = feed.items.find_by_guid(item.guid)
if old_item = feed.items.find_by(guid: item.guid)
old_item.increment(:version)
unless almost_same(old_item.title, item.title) and almost_same((old_item.body || "").html2text, (item.body || "").html2text)
old_item.stored_on = item.stored_on
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/about_controller_spec.rb
Expand Up @@ -8,7 +8,7 @@
describe 'GET /' do
context 'exists url' do
before do
allow(Feed).to receive(:find_by_feedlink) { @feed }
allow(Feed).to receive(:find_by).with(feedlink: @feed.link) { @feed }
get :index, url: @feed.link
end

Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/api/feed_controller_spec.rb
Expand Up @@ -117,7 +117,7 @@

describe 'POST /fetch_favicon' do
it 'renders json' do
allow(Feed).to receive(:find_by_feedlink).with(@feed.feedlink).and_return(@feed)
allow(Feed).to receive(:find_by).with(feedlink: @feed.feedlink).and_return(@feed)
expect(@feed).to receive(:fetch_favicon!)
post :fetch_favicon, { feedlink: @feed.feedlink }, { member_id: @member.id }
expect(response.body).to be_json
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/favicon_controller_spec.rb
Expand Up @@ -11,7 +11,7 @@

describe 'GET /' do
it "should send favicon" do
expect(Feed).to receive(:find_by_feedlink).with(@feed.link) { @feed }
expect(Feed).to receive(:find_by).with(feedlink: @feed.link) { @feed }
expect(controller).to receive(:send_data).with(anything, image_header) {
@controller.render nothing: true
}
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/rpc_controller_spec.rb
Expand Up @@ -67,7 +67,7 @@
expect {
post :update_feed, params.merge(api_key: member.auth_key)
}.to change {
Item.find_by_guid(params[:link]).nil?
Item.find_by(guid: params[:link]).nil?
}.from(true).to(false)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/favicon_spec.rb
Expand Up @@ -5,7 +5,7 @@
let!(:feed) { FactoryGirl.create(:feed) }

it 'returns favicon' do
expect(Feed).to receive(:find_by_feedlink).with(feed.link).and_call_original
expect(Feed).to receive(:find_by).with(feedlink: feed.link).and_call_original
get "/favicon/#{feed.link}"
expect(response).to be_ok
expect(response.content_type).to eq('image/png')
Expand Down

0 comments on commit 921cb4a

Please sign in to comment.