Last active
August 29, 2015 13:58
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Logo < ActiveRecord::Base | |
versioned | |
has_attached_file :image, | |
:styles => { :medium => "400x400#" } | |
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/ | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add paperclip | |
gem 'paperclip', '~>3.4.2' | |
# add vestal versions | |
gem 'vestal_versions', :git => 'git://github.com/laserlemon/vestal_versions' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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