Skip to content

Instantly share code, notes, and snippets.

@alvir
alvir / create_listing_by_url.rb
Created July 4, 2012 13:08
Smells of our city
def create_listing_by_url(url, who_creates_listing)
return { } if url.match(UrlsProcessor::URL_WITH_DOMAIN_ONLY)
initial_url = url
res = create_listing_matched_to_feed_by_url(url, who_creates_listing, initial_url)
return res if res
res = Listing.create_empty_result_hash
source_info = Scraper::Manager.detect_top_level_source(url)
unless source_info[:source]
res[:not_found] << '' # it's needed to make res[:not_found] not empty array
@alvir
alvir / gist:3197130
Created July 29, 2012 09:48
Funny scopes
class Feed < ActiveRecord::Base
scope :searchable, where(some_conditions)
scope :matchable, lambda {
where("url in
(SELECT du.url from
(SELECT url, count(*) cc
FROM feeds
WHERE (#{Feed.searchable_conditions})
GROUP by url
@alvir
alvir / array_with_if.rb
Created November 2, 2012 09:26
Typical smell. Conditions in extended array
def listed_date_links(feed_search, since_last_visit = false)
['Any', FeedSearch::SINCE_LAST_VIEW_VALUE, *FeedSearch::LAST_DAYS_VALUES].each do |col|
if col == current_listed_date_column(feed_search, since_last_visit).to_s
concat content_tag(:div, title_listed_date(col), :class => 'active')
else
concat link_to(title_listed_date(col), '#', :column_name => col, :class => 'subheader_link listed_date_column')
end
end
end
@alvir
alvir / signum.rb
Created October 2, 2013 06:42
Magic signum
def self.percent_by_prices(previous_price, current_price)
return 0 if previous_price.nil? || previous_price.zero?
res = (((current_price.to_f/previous_price.to_f) - 1) * 100).round
res = self.signum(current_price - previous_price) if res.zero?
res
end
def self.signum(number)
case
when number > 0
@alvir
alvir / raw_pluck.rb
Created March 6, 2014 09:36
Raw pluck as alternatives pluck
class ActiveRecord::Base
def self.raw_pluck(*args)
sql = connection.unprepared_statement { select(args).to_sql }
connection.execute(sql).to_a
end
end
@alvir
alvir / _partial.html.erb
Last active August 29, 2015 13:58
Instance variables in sub partials
<%= render 'subpartial' %>
<%= @subpartial_instance_variable %>
@alvir
alvir / file_ubuntu.sh
Created May 1, 2014 11:21
Problem with word file mime-type
$ file -b --mime-type 'Title_Of_Word_File.doc'
ERROR: (null)
$ file --version
file-5.09
magic file from /etc/magic:/usr/share/misc/magic
$ apt-get update
$ apt-get install file
@alvir
alvir / capybara-screenshot.zsh
Last active February 12, 2018 00:49
Open last file of capybara screenshots
# Put it to custom folder of ohmyzsh
# Open recent file of capybara screenshots
function ole(){
local NUMBER=1
if ! [ -z $1 ]; then
NUMBER=$1
fi
open tmp/capybara/*.png(On[$NUMBER])
}
alias ggrb='grb --committer-date-is-author-date origin/$(git_current_branch)'