Skip to content

Instantly share code, notes, and snippets.

@Joseph-N
Last active August 29, 2015 13:58
Show Gist options
  • Save Joseph-N/10240492 to your computer and use it in GitHub Desktop.
Save Joseph-N/10240492 to your computer and use it in GitHub Desktop.
How to implement revision control in rails app using paperclip and vestal versions - Tutorial link:- http://josephndungu.com/tutorials/revision-control-with-paperclip-rails
<%= form_for(@logo, :html => { :multipart => true }) do |f| %>
<% if @logo.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@logo.errors.count, "error") %> prohibited this logo from being saved:</h2>
<ul>
<% @logo.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.file_field :image %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
class Logo < ActiveRecord::Base
versioned
has_attached_file :image,
:styles => { :medium => "400x400#" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
# add paperclip
gem 'paperclip', '~>3.4.2'
# add vestal versions
gem 'vestal_versions', :git => 'git://github.com/laserlemon/vestal_versions'
<p id="notice"><%= notice %></p>
<p>
<%= image_tag @logo.image.url(:medium) %>
</p>
<p>
<strong>Name:</strong>
<%= @logo.name %>
</p>
<p>
<% if @logo.version > 1 %>
<%= link_to "Previous version", :version => @logo.version-1 %>
<% end %>
<% if params[:version] %>
<%= link_to "Latest version", :version => nil %>
<% end %>
</p>
<%= link_to 'Edit', edit_logo_path(@logo) %> |
<%= link_to 'Back', logos_path %> |
Version <%= @logo.version %>
class Logo < ActiveRecord::Base
versioned
has_attached_file :image,
:keep_old_files => true,
:styles => { :medium => "400x400#" },
:url => "/assets/logos/:id/versions/:version/:style/:basename.:extension",
:path => ":rails_root/public/assets/logos/:id/versions/:version/:style/:basename.:extension"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
# keep versions
Paperclip.interpolates :version do |attachment, style|
attachment.instance.version.to_s
end
end
<p id="notice"><%= notice %></p>
<p>
<%= image_tag @logo.image.url(:medium) %>
</p>
<p>
<strong>Name:</strong>
<%= @logo.name %>
</p>
<%= link_to 'Edit', edit_logo_path(@logo) %> |
<%= link_to 'Back', logos_path %> |
Version <%= @logo.version %>
<p id="notice"><%= notice %></p>
<p>
<%= image_tag @logo.image.url(:medium) %>
</p>
<p>
<strong>Name:</strong>
<%= @logo.name %>
</p>
<p>
<% if @logo.version > 1 %>
<%= link_to "Previous version", :version => @logo.version-1 %>
<% end %>
<% if params[:version] %>
<%= link_to "Latest version", :version => nil %>
<% end %>
</p>
<%= link_to 'Edit', edit_logo_path(@logo) %> |
<%= link_to 'Back', logos_path %> |
Version <%= @logo.version %>
<p id="notice"><%= notice %></p>
<p>
<%= image_tag @logo.image.url(:medium) %>
</p>
<p>
<strong>Name:</strong>
<%= @logo.name %>
</p>
<p>
<% if @logo.version > 1 %>
<%= link_to "Previous version", :version => @logo.version-1 %>
<% end %>
</p>
<%= link_to 'Edit', edit_logo_path(@logo) %> |
<%= link_to 'Back', logos_path %> |
Version <%= @logo.version %>
class LogosController < ApplicationController
before_action :set_logo, only: [:show, :edit, :update, :destroy]
# more actions
# GET /logos/1
# GET /logos/1.json
def show
@logo.revert_to(params[:version].to_i) if params[:version]
end
# GET /logos/new
def new
@logo = Logo.new
end
# more code follows
end
class LogosController < ApplicationController
before_action :set_logo, only: [:show, :edit, :update, :destroy]
# more code here
private
# Use callbacks to share common setup or constraints between actions.
def set_logo
@logo = Logo.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def logo_params
params.require(:logo).permit(:name, :image)
end
end
# config/initializers/versioning_with_paperclip.rb
module Paperclip
class Attachment
def save
flush_deletes unless @options[:keep_old_files]
flush_writes
@dirty = false
true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment