Skip to content

Instantly share code, notes, and snippets.

View tanraya's full-sized avatar

Andrew Kozlov tanraya

  • Astana, Kazakhstan
View GitHub Profile
def authorize!(oauth_hash, user)
User::Oauth2Service.new(oauth_hash, user).workflow self do
# Account successfully created, or user successfully logged in. Now sign in user.
on_sign_in do |account, provider_title|
session.delete("omniauth.auth")
session.delete("provider_title")
if account.user.confirmed?
sign_in_user_and_redirect(account.user, provider_title)
else
# Oauth-specific user methods
class User::Oauth2Service
def initialize(auth, user = nil)
@policy = policy(auth)
@user = user || User.where(email: @policy.email).first_or_initialize
@account = Oauth2::Account.where(provider: @policy.provider, uid: @policy.uid).first_or_initialize
end
def workflow(controller, &block)
@controller = controller
# encoding: utf-8
module Papillon::Controller
extend ActiveSupport::Concern
module ClassMethods
# TODO Write specs this
mattr_accessor :papillon_dsl, :papillon_resource, :papillon_controller_builder
def papillon(resource_class, options = {}, &block)
self.papillon_dsl ||= Papillon::Dsl.new(options, &block)
module Papillon
class Railtie < Rails::Railtie
initializer "papillon.action_controller" do
ActiveSupport.on_load(:action_controller) do
ActionController::Base.send :include, Papillon::Controller
end
end
end
end
# Render index page
class IndexFor
DEFAULT_OPTIONS = {
by: nil,
pagination: :bottom, # top, bottom, both
}.with_indifferent_access
attr_reader :resource, :options, :block, :h
def initialize(resource, options = {}, view_context, &block)
@tanraya
tanraya / gist:8585004
Last active January 4, 2016 06:58
Parse and compose urls to YouTube and Vimeo videos
module Url
module Parser
# Youtube
YOUTUBE_REGEX = [
/watch\?v=(?<slug>[A-Za-z0-9_-]+)/,
/(?:www\.)?youtube\.com\/(?:embed|v)\/(?<slug>[A-Za-z0-9_-]+)/,
/youtu\.be\/(?<slug>[A-Za-z0-9_-]+)/,
]
# Vimeo
// With SVG.js
var width = 500
var height = 20
var lineHeight = 3
var step = 10
var draw = SVG('canvas')
var rect = draw.rect(width, height).fill('#f09')
for (var i = 1; i < width/step; i++) {
# Организация
class Organization < ActiveRecord::Base
has_many :organization_members, class_name: 'Organization::Member', dependent: :destroy
has_many :members, through: :organization_members
attr_accessible :name
end
# Член организации
class Organization::Member < ActiveRecord::Base
@tanraya
tanraya / gist:7438337
Created November 12, 2013 20:44
Carrierwave auto orient image explained
module CarrierWave
module MiniMagick
# Rotates the image based on the EXIF Orientation
# According to http://jpegclub.org/exif_orientation.html
def auto_orient
manipulate! do |image|
case image['EXIF:Orientation'].to_i
when 2
image.flop
when 3
%w[
CollectionSelectInput
DateTimeInput
GroupedCollectionSelectInput
NumericInput
PasswordInput
RangeInput
StringInput
TextInput