releu

Force showing scroll on Mac with css by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

As we know we don't see scrolls on #OSX Lion and above by default, but sometimes we have to show them to user because of #UX.

We can do it just by adding scroll's styles. For example:

#myelement::-webkit-scrollbar {
  -webkit-appearance: none;
  width: 8px;
}

#myelement::-webkit-scrollbar-track {
  background-color: #eee;
  border-radius: 8px;
}
#myelement::-webkit-scrollbar-thumb {
  border-radius: 8px;
  background-color: #666;
}

#html

Custom helper methods of State Machine (ruby) by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

I have a lot of code in my project like the following:

class Animal < ActiveRecord::Base
  state_machine do
    around_transition :on => :sure do |animal, _, block|
      animal.transaction do
        block.call
        animal.do_things_on_sure
      end
    end
  end
end

around_transition looks ugly and repeats for many classes.. Lets refactor it. More under the cut

Using external resources in callbacks in rails by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

Here is a typical bad case in #rails:

class Band < ActiveRecord::Base
  has_many :members, dependent: :destroy
end

class Member < ActiveRecord::Base
  belongs_to :band

  after_destroy do
    Resque.enqueue BadNewsWorker, id
    raise('surprise') # real world :(
  end
end

band = Band.create
band.members.create
band.destroy # => RuntimeError
Band.count => 1

More under the cut

Using capture in templates by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

You can refactor template's code with capture method:

/ before:
li
  = mail_to 'info@gistflow.com', '<i class="icon-envelope"></i><span>Email</span>'.html_safe, class: 'secondary', encode: 'hex'

/ after:
li
  - name = capture do
    i class="icon-envelope"
    span Email
  = mail_to 'info@gistflow.com', name, class: 'secondary', encode: 'hex'

Very useful :)

Ruby on Rails API

#rails

How to create a triangle with css by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

When you wanna create a simple triangle you probably don't want to create a new image, then create a double-sized version for retina and so on..

There is a solution for triangles - css borders. They can be used for creating any triangles you need :) More under the cut

Be careful with UTF-8 regexp in ruby by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

I found the following:

# coding: utf-8
alphabet = "абвгдеёжзийклмнопрстуфхцчшщэюя" # (russian cyrillic)
alphabet =~ /\A[а-я]+\z/ # => nil

For some reason [а-я] doesn't include ё character.

I changed my #regexp to /\A[а-яё]+\z/, but it doesn't look great.

Is there another solution?

#ruby

A snippet for enabling maximum of checked checkboxes by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

Just wrap your inputs with <div data-max-values="3">...</div> and add the following #js

$('div[data-max-values]').on 'change :checkbox', ->
  condition = $(':checked', @).length >= Number($(@).data('max-values'))
  $(':not(:checked)', @).prop 'disabled', condition
$('div[data-max-values]').each (i, e) ->
  $(':checkbox:first', e).trigger('change')

#query required. #html #forms

Debug with css styles with poltergeist by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

When you migrated to #poltergeist you see annoying plain html debug when use save_and_open_page..

But we can cheat a little and do the following thing:

# env/test.rb
config.action_controller.asset_host = "http://jour_project.dev"
# or "http://0.0.0.0:3000" for example

Now you will get html generated in test env and assets from development env. Useful!

#rails #capybara #phantomjs

What is !=~ in ruby? by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

So we know about a =~ operator.

"foo" =~ /oo/ # => 1

But what about !=~?

This syntax doesn't raise any errors and returns boolean true value..

"foo" !=~ /foo/ # => true
"foo" !=~ /asd/ # => true

Does anybody can explain me how it works?

#ruby

Sorting by boolean fields in sql by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

Here is a trick I have found:

Assume we have a table abilities with field available.

select abilities.* from abilities order by 
(case when available then 1 when available is null then 2 else 3 end) asc

It replaces true as 1, null as 2 and false as 3 and then sorts by integers.

Do you know another solution?

#sql

Amazing ruby arguments by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

Just see the code:

# you can use args
def link_to_project(project, name = project.name)
  link_to_if may?(:manage, :projects), name, project
end

# instead of additional checking
def link_to_project(project, name = nil)
  name ||= project.name
  link_to_if may?(:manage, :projects), name, project
end

link_to_project @project
link_to_project @project, 'open'

#ruby #refactoring