Language plural rules (includes I18n)

Table with plural rules: Читать дальше »

 

Rails 4.1 html and tags chars sanitize

To remove all tags and chars like ‘nbsp;’ in rails 4.1 do that:
= sanitize(post.description).truncate(350, :separator => " ").html_safe
 

Rails timezones difference

  • In Rails, Time.zone refers to the Rails (not system) timezone, set by config.time_zone (typically in application.rb)
  • DateTime and Time are not otherwise Rails aware, therefore, DateTime.now and Time.now both return times in the system timezone.
  • 1.month.ago and similar methods use the Rails timezone, but DateTime.now.last_month uses the system timezone
  • When retrieving things from ActiveRecord, they will be timezoned to the Rails timezone, so Product.first.created_at will give you a time in the Rails timezone (not the system timezone)

So, don’t mix Rails timezone methods like “1.month.ago” with system timezone methods like “DateTime.now.last_month”

 

Отправка формы с валидацией, задержкой и с текстом благодарности

$('form').submit(function (e) {
    var form = this;
    if ($(this).find('.phone_number').val().length) {
      e.preventDefault();
      $(this).hide(300);
      $(this).parent().find('.thank_message').show(300);
      setTimeout(function () {
          form.submit();
      }, 3000); // in milliseconds
    } else {
      e.preventDefault();
      $(this).find('.phone_number').addClass('invalid_empty');
    }
  });
 

Fast and Simple youtube embedding

You can embed as many videos as you want and it still be fast.

HTML:

  <div class="video_name">Test video</div>
   <div class="video" style="background-color:#ccc; width:420px; height:315px;">
      <a href="http://www.youtube.com/watch?v=VIDEO_CODE" class="youtube"></a>
  </div>

JS:

<script type="text/javascript">
    function getYoutubeID(url) {
        var id = url.match("[\\?&]v=([^&#]*)");
        id = id[1];
        return id;
    };
    $('a.youtube').each(function() {
        var id = getYoutubeID( this.href );
        this.id = id;
        var thumb_url = "http://img.youtube.com/vi/"+id+"/0.jpg";
        $('<img width="100%" src="'+thumb_url+'" />').appendTo($(this.parentNode));

    });

    $('.video').on('click', function(e) {
        var link = $(this).find('a')[0];
        var id = getYoutubeID( link.href );
        $(this).html('<iframe width="420" height="315" src="//www.youtube.com/embed/'+ id +'?autoplay=1" frameborder="0" allowfullscreen></iframe>').css('background', 'none');
    });
</script>
 

MODx truncate function

Создаем сниппет с именем phx:character_limit

<?php
if (strlen($output) > $options) {
  $output_cutted = substr($output, 0, $options);
  $last_space = strrpos($output_cutted, " ");
  $output = substr($output_cutted, 0, $last_space) . " ...";
        preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $output, $result);
    $openedtags = $result[1];

    #put all closed tags into an array
    preg_match_all('#</([a-z]+)>#iU', $output, $result);
    $closedtags = $result[1];
    $len_opened = count($openedtags);

    # all tags are closed
    if (count($closedtags) == $len_opened) {
        return $output;
    }
    $openedtags = array_reverse($openedtags);

    # close tags
    for ($i=0; $i < $len_opened; $i++) {
        if (!in_array($openedtags[$i], $closedtags)){
            $output .= '</'.$openedtags[$i].'>';
        } else {
            unset($closedtags[array_search($openedtags[$i], $closedtags)]);
        }
    }
  }
return $output;
?>

Пример вызова:

[+content:character_limit=`300`+]
 

The way to store config options in ruby without using ENV variables

# my_project/config.rb
module MyProject
  class Config
    attr_accessor :my_cool_option, :my_other_option
  end
end

# my_project.rb
module MyProject
  def self.config
    @config ||= Config.new
  end

  def self.configure
    yield(config)
  end