Skip to content

Instantly share code, notes, and snippets.

View DamirSvrtan's full-sized avatar
🎖️

Damir Svrtan DamirSvrtan

🎖️
View GitHub Profile

Hey everyone,

For a long time I've been writing boilerplate classes that have basically the same structure of initialization:

class UserRegistrationForm  
  def initialize(access_code)
    @access_code = access_code
  end
 
require "rails_helper"
RSpec.describe PokemonsController do
describe "GET #index" do
it "renders the index template" do
get :index
expect(response.status).to eq(200)
expect(response).to render_template('index)
end
end
@DamirSvrtan
DamirSvrtan / app_template.rb
Last active August 29, 2015 14:05
Rails application generator template
### Rails app generator template. Run it:
### rails new _app_name_ -m https://gist.githubusercontent.com/DamirSvrtan/28a28e50d639b9445bbc/raw/app_template.rb
remove_file "README.rdoc"
create_file "README.md", "Development: run ./bin/setup"
run 'mv app/assets/stylesheets/application.css app/assets/stylesheets/application.scss'
database_type = ask("Do you want to use postgres or mysql?", limited_to: ["pg", "mysql"])
adapter = if database_type == 'pg'
@DamirSvrtan
DamirSvrtan / server-setup.sh
Last active August 29, 2015 14:03
Basic Server Setup
pretty_echo() {
echo_length=${#1}
stars=""
star_length=$(($echo_length+8))
for i in $(seq 1 $star_length); do
stars="$stars*"
done
echo "\n"; echo "$stars"; echo "*** $1 ***"; echo "$stars"; echo "\n"
};
```ruby
"SERVER_SOFTWARE"=>"thin 1.6.2 codename Doc Brown",
"SERVER_NAME"=>"localhost",
"rack.version"=>[1, 0],
"rack.errors"=>#<IO:<STDERR>>,
"rack.multithread"=>false,
"rack.multiprocess"=>false,
"rack.run_once"=>false,
"REQUEST_METHOD"=>"GET",
"REQUEST_PATH"=>"/",
@DamirSvrtan
DamirSvrtan / gist:ae294ddf30f4ab740473
Last active August 29, 2015 14:01
Redis Store Stormpath
stormpath_client = Stormpath::Client.new(api_key: api_key, cache: {
regions: {
applications: { store: Stormpath::Cache::RedisStore, ttl_seconds: 300, tti_seconds: 300, store_opts: {:host => uri.host, :port => uri.port, :password => uri.password} },
directories: { store: Stormpath::Cache::RedisStore, ttl_seconds: 300, tti_seconds: 300, store_opts: {:host => uri.host, :port => uri.port, :password => uri.password} },
groups: { store: Stormpath::Cache::RedisStore, ttl_seconds: 300, tti_seconds: 300, store_opts: {:host => uri.host, :port => uri.port, :password => uri.password} }
}
})
Returning an array as a response:
[200, {'Content-Type' => 'text/html'}, ["<h1>Hello!</h1>"]]
Benchmark:
10000 runs
0.037738 seconds total
0.0037738 millisec/run
With a controller rendering a template with erb:
@DamirSvrtan
DamirSvrtan / rails_flash_messages.rb
Created March 29, 2014 16:41
Rails Flash Messages Helper
def flash_messages
content_tag :div, class: "flash-messages" do
flash.map do |key, value|
content_tag :div, class: "alert alert-dismissable alert-#{key}" do
content_tag(:span, '&times;'.html_safe, class: :close, 'data-dismiss' => 'alert') + value
end
end.join().html_safe
end
end
@DamirSvrtan
DamirSvrtan / aliases.sh
Last active January 4, 2016 01:49
Aliases
alias productive='git log --since="today" --oneline | sed "s/^\w\+/*/g"'
@DamirSvrtan
DamirSvrtan / nojz
Last active February 2, 2023 21:58
Bluetooth speaker connection script. Out of convenience written in Ruby.
#!/usr/bin/env ruby
require 'open3'
class NojzError < StandardError
def initialize(msg)
@msg = msg
super(msg)
end