Skip to content

Instantly share code, notes, and snippets.

@jun1st
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jun1st/c3a684ebdbb9c61abe7e to your computer and use it in GitHub Desktop.
Save jun1st/c3a684ebdbb9c61abe7e to your computer and use it in GitHub Desktop.
class Article < ActiveRecord::Base
include Shareable
include Subscribable
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def more_like_this
Topic.search(
query: {
more_like_this: {
fields: ['title', 'content'],
like_text: title + '\n' + body
}
},
filter: {
and: [
{ term: { trashed: false } },
{ not: { term: { id: id } } }
]
}
)
end
end
module Shareable
extend ActiveSupport::Concern
included do
has_many :shares, as: 'shareable', dependent: :delete_all
end
def shared_by?(user)
shares.where(user: user).exists?
end
end
class SharesController < ApplicationController
before_filter :login_required, :no_locked_required, :find_share
def create
@shareable.shares.find_or_create_by_user_id current_user.id
end
def destroy
@shareable.shares.where(user: current_user).destroy_all
end
private
def find_share
resource, id = request.path.split('/')[1, 2]
@shareable = resource.singularize.classify.constantize.find(id)
end
end
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
helper_method :login?, :current_user
def login_required
unless login?
redirect_to login_url(return_to: (request.fullpath if request.get?))
end
end
def no_login_required
if login?
redirect_to root_url
end
end
def current_user
@current_user ||= login_from_session || login_from_cookies unless defined?(@current_user)
@current_user
end
def login?
!!current_user
end
def store_location(path)
session[:return_to] = path
end
def redirect_back_or_default(default)
redirect_to(session.delete(:return_to) || default)
end
def remember_me
cookies[:remember_token] = {
value: current_user.remember_token,
expires: 2.weeks.from_now,
httponly: true
}
end
end
module RedisIndexable
extend ActiveSupport::Concern
REDIS_KEY = "Neonan:#{Rails.env}:articles_videos_index"
included do
after_save :save_to_redis
after_destroy :delete_from_redis
end
def save_to_redis
if self.status == 0 && self.status_changed?
sorted_set = Redis::SortedSet.new(REDIS_KEY)
sorted_set.delete("#{self.class.to_s}_#{self.id}")
end
if self.status == 1 && self.published_at_changed?
sorted_set = Redis::SortedSet.new(REDIS_KEY)
sorted_set["#{self.class.to_s}_#{self.id}"] = self.published_at.to_i
end
end
def delete_from_redis
sorted_set = Redis::SortedSet.new(REDIS_KEY)
sorted_set.delete("#{self.class.to_s}_#{self.id}")
end
end
#matrix test, new algorithm
require 'matrix'
require 'minitest/autorun'
class Matrix
def sequence_sum(sequence_matrix)
raise 'the sequence must be a matrix' unless sequence_matrix.is_a? Matrix
raise 'the sequence matrix doesn\'t mathch target' unless sequence_matrix.row_count == self.column_count and sequence_matrix.row_count == self.column_count
# ordered_sequence = reorder_sequence_matrix(sequence_matrix)
# ordered_sequence.reduce(0) do |sum, item|
# val = self.component(item[1][0], item[1][1]) || ''
# sum + val.to_i
# end
sequence_array = sequence_array(sequence_matrix)
sequence_array.reduce(0) do |sum, item|
val = self.component(item[0], item[1]) || ''
sum + val.to_i
end
end
private
# def reorder_sequence_matrix(sequence_matrix)
# ordered_sequence = {}
# sequence_matrix.each_with_index do |s, row, col|
# ordered_sequence[s] = [row, col]
# end
# Hash[ordered_sequence.sort]
# end
def sequence_array(sequence_matrix)
sequence_array = []
sequence_matrix.each_with_index do |s, row, col|
sequence_array[s - 1] = [row, col]
end
sequence_array
end
end
describe Matrix do
before do
@matrix_to_sum = Matrix[[12, 32, '09', 11, 34], ['08', 54, 76, 23, 07], [27, 18, 25, '09', 43] ,[11, 23, 78, 63, 19], ['09', 22, 56, 31, '05']]
@sequence_matrix = Matrix[[1, 2, 3, 4, 5], [16, 17, 18, 19, 6], [15, 24, 25, 20, 7], [14, 23, 22, 21, 8], [13, 12, 11, 10, 9]]
end
describe "when doing sequencial sum" do
it "should produce same sum" do
sum = @matrix_to_sum.reduce(0) { |sum, val| sum += val.to_i }
@matrix_to_sum.sequence_sum(@sequence_matrix).must_equal sum
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment