Skip to content

Instantly share code, notes, and snippets.

View schovi's full-sized avatar
👁️

David Schovanec schovi

👁️
View GitHub Profile

Začnu obecným použitím angularu v aplikaci bez jakýchkoli specialit. Kód v šabloně, na který se aplikuje Angular označíme atributem ng-app. Angular pak projde celý vnitřní DOM a koná svoje. Proto není vždy výhodné dát ng-app na BODY.

Minimální ukázka Angular aplikace

<div ng-app>
  <span ng-click="hodnota = 'befeleme'">Reaguju na klik a nastavím proměnou 'hodnota'</span>
  moje proměná je {{hodnota}}
</div>
@schovi
schovi / react-footprint.js
Created March 1, 2015 18:49
React component unique footprint
// For given component/children generates uniqe footprint.
// For now it just contains type of elements and text from text nodes in plain array
// TODO: make footprint from element.type and element.props for 100% precision
var React = require('react/addons')
function makeFootprint(children) {
var result = []
return getComponentsKeys(result, children)
}
@schovi
schovi / routeState.js
Created July 16, 2015 12:01
routeState.js
import express from 'express';
const router = express.Router();
router.route('/products')
.post((req, res, next) => {
req.routeState = {
products: [
{id: 1, name: "Bike"}
]
en:
activerecord:
models:
region:
one: "Country"
other: "Countries"
image:
one: "Photo"
other: "Photos"
project_species:
@schovi
schovi / index.js
Last active September 13, 2015 17:36
Require for custom extensions.
var fs = require('fs')
require.extensions['.png'] = function(require, path) {
require.exports = fs.readFileSync(path)
}
var image = require("./image.png")
// Image is Buffer
@schovi
schovi / gist:626293
Created October 14, 2010 14:51
Rails 3 scope for select model depend on tags. Include selecting more tags ( && or || for them)
scope :tagged_with, lambda { |tags, match_all = false|
tag_array = tags.is_a?(Array) ? TagList.new(tags) : TagList.from(tags)
where_operator = match_all ? :& : :|
where_conditions = tag_array.collect {|name| {:tags => :name =~ name} }
where_sql = where_conditions.inject(&where_operator)
select("DISTINCT prints.*").joins(:tags).where(where_sql)
}
@schovi
schovi / gist:665326
Created November 6, 2010 10:19
Rail3 friendly pagination
# If you are using ActiveRecord and want to paginate it you have to write this:
# PeacefulPaginate.use_for_active_record!
# to end of your config/environment.rb
# If you want to override default .paginate method from WillPaginate gem you have to insert this
# PeacefulPaginate.use_compatible_mod_with_will_paginate!
# to end of your config/environment.rb
# With this you dont have to rewrite nothing else in your application
# In other way how to use it
@schovi
schovi / active_record_mass_assign_attr_for_sti.rb
Created November 25, 2010 19:13
How to mass assign attribute 'type' for STI with validation
class Order < ActiveRecord::Base
validates_inclusion_of :type, :in => %w{SpecialOrder}
def self.new(params = {})
_type = params.delete(:type)
_new = super(params)
_new.type = _type.to_s.camelize if _type
return _new
end
@schovi
schovi / gist:734113
Created December 8, 2010 23:34
idea of chaining error validations
class UltraModel < ActiveRecord::Base
validates_presence_of :super_id
validates_format_of :super_id, :dependency => :super_id_presence
validates_uniqueness_of :super_id, :dependency => :super_id_format
validates :super_object_exist, :dependency => :super_id_uniqueness
private
def super_object_exist
@schovi
schovi / gist:763475
Created January 3, 2011 13:44
Mechanize in action
require 'rubygems'
require 'mechanize'
require 'fileutils'
def start
a = Mechanize.new
last_file = Dir.glob('wulf*.*')[-1]
if last_file and /.*?_(?<year>\d{4})\-(?<month>\d{2})-(?<day>\d{2})/ =~ last_file
strip = "#{year}/#{month}/#{day}"