Skip to content

Instantly share code, notes, and snippets.

View asiniy's full-sized avatar

Alex Antonov asiniy

View GitHub Profile
@asiniy
asiniy / my_reelty.ex
Last active August 11, 2016 11:17
MyReelty migration commands
# rsync files
rsync -rv --include '*/' --include '*.jpg' --exclude '*' --prune-empty-dirs [email protected]:~/videos/ ~/
rsync -rv --prune-empty-dirs [email protected]:~/images/ ~/
# change backup
mix ecto.drop ; mix ecto.create ; psql my_reelty_dev < ~/development/my_reelty/production/backup.sql
# Remove all reviews except last 200
required_ids = Ecto.Query.from(r in Review, limit: 200, order_by: [desc: :id], select: [:id], where: is_nil(r.deleted_at)) |> Repo.all |> Enum.map(fn(r) -> r.id end)
Ecto.Query.from(r in Review, where: not(r.id in ^required_ids), order_by: [asc: :id]) |> Repo.update_all(set: [deleted_at: Ecto.DateTime.utc])
@asiniy
asiniy / sources.rb
Created October 22, 2017 16:08
Rails sources
class DownloadController < ActionController::API
def bill
user_id = DownloadToken.verify(params[:token])
return head :unprocessable_entity if user_id.nil?
user = User.find_by(id: user_id)
return head :not_found if user.nil?
member = Member.find_by(id: params[:id])
class Movie < ActiveRecord::Base
has_many :sessions
def self.next_week_list
next_week_beginning = Date.today.at_beginning_of_week.next_week
next_week_end = Date.today.at_end_of_week.next_week
joins(:session).where("sessions.beginning_of > ? and sessions.beginning_of < ?", next_week_beginning, next_week_end)9
end
end
@asiniy
asiniy / propTypes.js
Last active July 3, 2018 15:19
propTypes // Extending JavaScript with ease (beginner level tutorial)
import React from 'react'
import PropTypes from 'react-proptypes'
class MyComponent extends React.Component {
// render logic here
}
MyComponent.propTypes = {
collectorGroup: PropTypes.shape({
target_count: PropTypes.number.isRequired,
@asiniy
asiniy / MyComponent.jsx
Created July 4, 2018 06:52
Initial code - Extending JavaScript with ease (beginner level tutorial)
import React from 'react'
INNER_CIRCLE_TARGETS_COUNT = 10
class MyComponent extends React.Component {
render() {
// A big function with a complex logic
const { collectorGroup } = this.props
const collectorGroupHasFewCircles = (collectorGroup.targets_count > INNER_CIRCLE_TARGETS_COUNT)
@asiniy
asiniy / hash_fetch.rb
Created July 4, 2018 07:10
Rubyish Hash#Fetch # Extending JavaScript with ease (beginner level tutorial)
o = { a: 'b' }
o.fetch(:a) # => returns 'b'
o.fetch(:nonexistant) # => throws prog.rb:2:in `fetch': key not found (KeyError)
@asiniy
asiniy / zealit.js
Last active July 4, 2018 07:40
zealit // Extending JavaScript with ease (beginner level tutorial)
import zealit from 'zealit'
// A reminder - collectorGroup looks like:
// const collectorGroup = { target_count: 10 }
const fetchedObject = zealit(collectorGroup)
fetchedObject.target_count // returns 10
fetchedObject.targets_count // throws a ReferenceError
@asiniy
asiniy / objectFetch.js
Created July 4, 2018 07:44
objectFetch // Extending JavaScript with ease (beginner level tutorial)
const objectFetch = (object, propertyName) => {
if (object.hasOwnProperty(propertyName)) {
return object[propertyName]
}
throw new ReferenceError(`key not found: ${propertyName}`)
}
const collectorGroup = { target_count: 10 }
@asiniy
asiniy / index.ts
Created July 4, 2018 11:23
index.js // Extending JavaScript with ease (beginner level tutorial)
// The only significant difference between TS and JavaScript is interface.
// Interface declarates that object passed as `obj` should be an object actually
interface PassedObject {
readonly [key: string]: any;
}
// Same logic as in our ES6 code
const objectFetch = (obj: PassedObject, property: string): any => {
// tslint:disable-next-line:no-if-statement
if (obj.hasOwnProperty(property)) {
@asiniy
asiniy / index.spec.ts
Created July 4, 2018 11:35
index.spec.ts // Extending JavaScript with ease (beginner level tutorial)
// tslint:disable:no-expression-statement
import { test } from 'ava';
import objectFetch from './index';
const object = { a: 'b' };
test('returns key if it exists', t => {
t.is(objectFetch(object, 'a'), 'b');
});