Skip to content

Instantly share code, notes, and snippets.

View AlexandrBasan's full-sized avatar
:octocat:
Building an effective team which works from home full-time

Alexandr Basan AlexandrBasan

:octocat:
Building an effective team which works from home full-time
View GitHub Profile
Open Rails console in jruby:
jruby -S bundle exec rails console
jRuby RailsCast
http://railscasts.com/episodes/376-jruby-basics
@AlexandrBasan
AlexandrBasan / _error_messages.erb
Created June 24, 2015 04:49
Add custom error using standard Rails error Model
<% if object and object.errors.any? %>
<div class="error_explanation">
<h2><%= pluralize(object.errors.count, "error") %> prohibited this <%= object.class.model_name.human.downcase %> from being saved:</h2>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
@AlexandrBasan
AlexandrBasan / app.js
Created June 12, 2015 02:18
Left sliding menu for website.
$('.icon-menu').click(function() {
$('.menu').animate({
left: "0px"
}, 200);
$('body').animate({
left: "285px"
}, 200);
});
@AlexandrBasan
AlexandrBasan / mailers_controller.rb
Created June 11, 2015 03:55
Mailer Viewer inside Rails application
class MailersController < Admin::BaseController
def render_layout
# received params
# params[:mailer_type]
# params[:email_template]
# params[:email_format]
mailer_type = params[:mailer_type].camelize
mailer_template = params[:email_template]
type_class = mailer_type.classify.safe_constantize
@AlexandrBasan
AlexandrBasan / gist:9e1260477bf54f6c44e4
Last active November 15, 2015 22:26
REGEX for find trailing whitespace
Ruby function for delete leading and trailing white-spaces:
2.1.5 :001 > " clean up my edges ".strip
=> "clean up my edges"
Regex for trailing white-spaces:
[ \t]+$
@AlexandrBasan
AlexandrBasan / gist:582cde2677be0f41beee
Created April 9, 2015 04:05
Credit card check using REGEX
def card_number_format
case card_type.downcase
when 'visa'
/^4\d{3}\d{4}\d{4}\d{4}$/
when 'mastercard'
/^5[1-5]\d{2}\d{4}\d{4}\d{4}$/
when 'american express'
/^3[4,7]\d{13}$/
end
end
@AlexandrBasan
AlexandrBasan / gist:949a774243c1c5d3a1e6
Created April 9, 2015 04:03
Ruby parser (schema.rb)
<% a = File.open("db/schema.rb") %>
<% a.each do |out| %>
<% if out.scan(/\s\s\s\st.[a-z]{1,12}\s{1,6}"(.*?)\"/) %>
<% if out.scan(/\s\s\s\st.[a-z]{1,12}\s{1,6}"(.*?)\"/).join(', ') == 'created_at' || out.scan(/\s\s\s\st.[a-z]{1,12}\s{1,6}"(.*?)\"/).join(', ') == 'updated_at' %>
<% else %>
:<%= out.scan(/\s\s\s\st.[a-z]{1,12}\s{1,6}"(.*?)\"/).join(', ') %>,
<% end %>
<% elsif out.scan(/\s\s\s\st.[a-z]{1,12}\s{1,6}"(.*?)\"\n\s\send/)%>
<% if out.scan(/\s\s\s\st.[a-z]{1,12}\s{1,6}"(.*?)\"\n\s\send/).join(', ') == 'created_at' || out.scan(/\s\s\s\st.[a-z]{1,12}\s{1,6}"(.*?)\"\n\s\send/).join(', ') == 'updated_at' %>
<% else %>
@AlexandrBasan
AlexandrBasan / gist:e428025c9e9f389fe17d
Created April 9, 2015 00:20
Ruby find all elements between two symbols
<% array_table = a.scan(/\((.*?)\ =>/) %>
<% array_index = a.scan(/\[(.*?)\]/) %>
<% com = "" %>
<% array_table.length.times do |element| %>
<% if com != array_table[element].join(', ')%>
<!-- Add commit -->
# <%= array_table[element].join(', ') %></br>
<% end %>
<!-- Text construction for output -->
add_index :<%= array_table[element].join(', ') %>, :<%= array_index[element].join(', ') %></br>
@AlexandrBasan
AlexandrBasan / gist:304cf54e45c7735a566e
Last active August 29, 2015 14:18
Ruby URL PARSER (find all http tags and img tags on page)
<% link_result = Array.new([]) %>
<% img_result = Array.new([]) %>
<% require 'open-uri' %>
<% uri = URI.parse('http://url.com') %>
<% data = uri.read %>
<% @links = data.scan(URI.regexp(%w(http https))) %>
</br>
<% @links.each do |element| %>
<%= text_field_tag nil, nil, :id => 'destination_name', data: {autocomplete_source: Airport.order(:name).map { |t| { :label => t.name, :value => t.id } } } %>
<%= text_field_tag nil, nil, :id => 'destination_id' %>
<script>
$("#destination_name").autocomplete({
source:$('#destination_name').data('autocomplete-source'),
select: function(e, ui) {
e.preventDefault() // <--- Prevent the value from being inserted.
$(this).val(ui.item.label)
$('#destination_id').val(ui.item.value)
}