Skip to content

Instantly share code, notes, and snippets.

View alexshagov's full-sized avatar
💭
I may be slow to respond.

Alexander Shagov alexshagov

💭
I may be slow to respond.
View GitHub Profile
@alexshagov
alexshagov / .rubocop.yml
Created October 6, 2015 15:49
Rubocop config
AllCops:
Include:
- "**/*.gemspec"
- "**/*.podspec"
- "**/*.jbuilder"
- "**/*.rake"
- "**/*.opal"
- "**/Gemfile"
- "**/Rakefile"
- "**/Capfile"
@alexshagov
alexshagov / terminal current branch.txt
Last active November 14, 2015 16:53
Show current branch in terminal
#add to your ~/.bashrc
#show current branch
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w\$\[\033[1;34m\]\$(parse_git_branch)\[\033[0m\] "

#Simple Authentication with Bcrypt

This tutorial is for adding authentication to a vanilla Ruby on Rails app using Bcrypt and has_secure_password.

The steps below are based on Ryan Bates's approach from Railscast #250 Authentication from Scratch (revised).

You can see the final source code here: repo. I began with a stock rails app using rails new gif_vault

##Steps

class AddFileToPicture < ActiveRecord::Migration
def change
add_attachment :pictures, :file
end
end
class Picture < ActiveRecord::Base
has_attached_file :file, styles: { medium: "300x300>" }
validates :file, :title, presence: true
validates_attachment_content_type :file, content_type: /\Aimage\/.*\Z/
end
Rails.application.routes.draw do
resources :pictures, only: [:index, :create, :new] do
put "sort", on: :collection
end
root 'pictures#index'
end
class PicturesController < ApplicationController
def index
@pictures = Picture.order(:priority)
end
def new
@picture = Picture.new
end
def create
<div class="container">
<h3><%= link_to "Add picture", new_picture_path %></h3>
<% @pictures.each do |picture| %>
<div class = "picture" data-id = "<%= picture.id %>">
<%= image_tag(picture.file.url(:medium)) %>
</div>
<% end %>
</div>
$(function(){
var updated_order = function(){
var data_arr = [];
$.each($(".picture"), function(index){
$(this).attr("priority", index);
data_arr.push({id: $(this).attr("data-id"), priority: index })
});
return data_arr;
}
gem 'virtus'
gem 'simple_form'
group :development, :test do
gem 'pry'
end