diff --git a/.gitignore b/.gitignore index c111b33..cbf49a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,11 @@ +.rvmrc +.sass-cache *.gem +*.swp +**/*.swp +Gemfile.lock +test_app/.bundle +test_app/Gemfile.lock +test_app/db/*.sqlite3 +test_app/log/*.log +test_app/tmp diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..4e1e0d2 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--color diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..e1db734 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'http://rubygems.org' + +gemspec + diff --git a/README.md b/README.md index 421c1de..689e386 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ On the controller you can render a view after user creation, create a simple cro <%= f.submit 'Save' %> <% end %> -Both helpers accept a :width option to customize their dimensions. The preview box has width 100 by default but the cropbox is unlimited in size (takes the original image width), so setting the cropbox width is interesting to avoid layout breaking with huge images. +Both helpers accept a :width option to customize their dimensions. The preview box has width 100 by default but the cropbox is unlimited in size (takes the original image width), so setting the cropbox width is interesting to avoid layout breaks with huge images. <%= form_for @user do |f| %> <%= f.cropbox :avatar, :width => 500 %> @@ -46,6 +46,11 @@ Both helpers accept a :width option to customize their dimensions. The preview b <%= f.submit 'Save' %> <% end %> -Regardless of the width passed, the preview box and the cropping area will have the aspect ratio defined in the model (1:1 by default) +Regardless of the width, the preview box and the cropping area will have the aspect ratio defined in the model (1:1 by default) -That's all! \ No newline at end of file +That's all! + +#### Credits and resources +* [Thoughtbot's Paperclip](https://github.com/thoughtbot/paperclip/) +* [Deep Liquid's JCrop](http://deepliquid.com/content/Jcrop.html) +* And Ryan Bates' [Railscast#182](http://railscasts.com/episodes/182-cropping-images/), which has inspired this gem \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..e5af5fa --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +require 'bundler' +Bundler::GemHelper.install_tasks + +require 'rspec/core' +require 'rspec/core/rake_task' +RSpec::Core::RakeTask.new(:spec) + +task :default => :spec \ No newline at end of file diff --git a/lib/assets/javascripts/papercrop.js b/lib/assets/javascripts/papercrop.js index e3ffcac..a9a95f7 100644 --- a/lib/assets/javascripts/papercrop.js +++ b/lib/assets/javascripts/papercrop.js @@ -1,3 +1,5 @@ +var jcrop_api; + (function() { $(document).ready(function() { $('div[id$=_cropbox]').each(function() { @@ -35,7 +37,7 @@ setSelect : [0, 0, 500, 500], aspectRatio : aspect, boxWidth : $("input[id$='_" + attachment + "_box_w']").val() - }); + }, function(){ jcrop_api = this; }); }); }); }).call(this); \ No newline at end of file diff --git a/lib/papercrop/active_record_extension.rb b/lib/papercrop/active_record_extension.rb index 7fd8503..9ebe4ee 100644 --- a/lib/papercrop/active_record_extension.rb +++ b/lib/papercrop/active_record_extension.rb @@ -40,7 +40,6 @@ def crop_attached_file(attachment_name, opts = {}) after_update :"reprocess_to_crop_#{attachment_name}_attachment" end - end @@ -66,6 +65,7 @@ def image_geometry(attachment_name, style = :original) end + # Uses method missing to reprocess the attachment callback def method_missing(method, *args) if method.to_s =~ /(reprocess_to_crop_)(\S{1,})(_attachment)/ reprocess_cropped_attachment( @@ -80,7 +80,7 @@ def method_missing(method, *args) # Reprocess the attachment after cropping def reprocess_cropped_attachment(attachment_name) - self.send(attachment_name.to_sym).reprocess! if cropping? attachment_name + self.send(attachment_name.to_sym).reprocess! if cropping?(attachment_name) end end diff --git a/lib/papercrop/controller_extension.rb b/lib/papercrop/controller_extension.rb deleted file mode 100644 index a7aecd6..0000000 --- a/lib/papercrop/controller_extension.rb +++ /dev/null @@ -1,29 +0,0 @@ -module Papercrop - module ControllerExtension - - def self.included(klass) - klass.extend ClassMethods - end - - - module ClassMethods - - def crop_image(field_name, opts) - opts[:after] ||= [:create, :update] - opts[:class] ||= self.class.name.gsub("Controller", "").singularize.constantize - end - - end - - - module InstanceMethods - end - end -end - - -# if defined? ActionController::Base -# ActionController::Base.class_eval do -# include Papercrop::ControllerExtension -# end -# end \ No newline at end of file diff --git a/papercrop.gemspec b/papercrop.gemspec index 0e154a6..7447af8 100644 --- a/papercrop.gemspec +++ b/papercrop.gemspec @@ -1,18 +1,25 @@ Gem::Specification.new do |s| s.name = 'papercrop' - s.version = '0.0.2' + s.version = '0.0.5' s.date = '2012-08-12' s.summary = "Paperclip extension for cropping images" - s.description = "Paperclip extension for cropping images" + s.description = "An easy extension for Paperclip to crop your image uploads using jCrop" s.authors = ["Ruben Santamaria"] s.email = 'rsantamaria.dev@gmail.com' s.homepage = 'https://github.com/rsantamaria/papercrop' s.files = Dir.glob("{lib,vendor}/**/*") + %w(README.md) + s.test_files = Dir.glob("{spec}/**/*") s.require_paths = ["lib"] - s.add_dependency "rails", "~> 3.1" + s.add_dependency "rails", ">= 3.1" s.add_dependency "jquery-rails" - s.add_dependency "paperclip", "~> 3.1" + s.add_dependency "paperclip", ">= 3.1" + + s.add_development_dependency "rspec-rails", "~> 2.0" + s.add_development_dependency "capybara", ">= 1.1.1" + s.add_development_dependency "rmagick" + s.add_development_dependency "sass" + s.add_development_dependency "sqlite3" end \ No newline at end of file diff --git a/spec/integration/papercrop_js_spec.rb b/spec/integration/papercrop_js_spec.rb new file mode 100644 index 0000000..85ea7c0 --- /dev/null +++ b/spec/integration/papercrop_js_spec.rb @@ -0,0 +1,29 @@ +require "spec_helper" + +describe "Image crop with JS", :js => true do + + after do + Landscape.destroy_all + end + + + it "crops an image with javascript" do + visit landscapes_path + + click_link "New Landscape" + + fill_in "Name", :with => "Mountains" + find("#landscape_picture").native.send_keys(File.expand_path("../../../test_app/test/fixtures/matterhorn.jpg", __FILE__)) + click_button "Create Landscape" + + sleep 2 + page.execute_script("jcrop_api.setSelect([300, 200, 700, 500])") + page.execute_script('if ($("#picture_crop_y").val() == "199"){$("#picture_crop_y").val("200")}') + page.execute_script('if ($("#picture_crop_w").val() == "399"){$("#picture_crop_w").val("400")}') + + click_button "Crop image" + + sleep 1 + compare_images(CROPPED_IMG_PATH, Landscape.last.picture.path(:medium)).should eq(0.0) + end +end \ No newline at end of file diff --git a/spec/integration/papercrop_spec.rb b/spec/integration/papercrop_spec.rb new file mode 100644 index 0000000..f1dadec --- /dev/null +++ b/spec/integration/papercrop_spec.rb @@ -0,0 +1,39 @@ +require "spec_helper" + +describe "Image crop" do + + after do + Landscape.destroy_all + end + + + it "crops an image" do + visit landscapes_path + + click_link "New Landscape" + + fill_in "Name", :with => "Mountains" + attach_file "Picture", "test_app/test/fixtures/matterhorn.jpg" + click_button "Create Landscape" + + page.should have_css("#picture_crop_preview_wrapper") + page.should have_css("#picture_crop_preview") + page.should have_css("#picture_cropbox") + + page.should have_css("#landscape_picture_original_w") + + find("#landscape_picture_original_w").value.should eq("1024.0") + find("#landscape_picture_original_h").value.should eq("768.0") + find("#landscape_picture_box_w").value.should eq("600") + find("#picture_aspect").value.should eq((4.0 / 3.0).to_s) + + find("#picture_crop_x").set "300.0" + find("#picture_crop_y").set "200.0" + find("#picture_crop_w").set "400" + find("#picture_crop_h").set "300" + + click_button "Crop image" + + compare_images(CROPPED_IMG_PATH, Landscape.last.picture.path(:medium)).should eq(0.0) + end +end \ No newline at end of file diff --git a/spec/model_extensions/active_record_spec.rb b/spec/model_extensions/active_record_spec.rb new file mode 100644 index 0000000..5b11235 --- /dev/null +++ b/spec/model_extensions/active_record_spec.rb @@ -0,0 +1,64 @@ +require "spec_helper" + +describe "Active Record Extension" do + + before do + @landscape = Landscape.new(:name => "Mountains") + @landscape.picture = open("test_app/test/fixtures/matterhorn.jpg") + @landscape.save + end + + + after do + @landscape.destroy + end + + + it "includes accessors to the model" do + %w{crop_x crop_y crop_w crop_h original_w original_h box_w aspect}.each do |m| + @landscape.should respond_to(:"picture_#{m}") + @landscape.should respond_to(:"picture_#{m}=") + end + end + + + it "registers the post processor" do + @landscape.attachment_definitions[:picture][:processors].should eq([:cropper]) + end + + + it "defines an after update callback" do + @landscape._update_callbacks.map do |e| + e.instance_values['filter'].should eq(:reprocess_to_crop_picture_attachment) + end + end + + + it "returns image properties" do + @landscape.picture_aspect.should eq(4.0 / 3.0) + + @landscape.image_geometry(:picture).width.should eq(1024.0) + @landscape.image_geometry(:picture).height.should eq(768.0) + end + + + it "knows when to crop" do + @landscape.cropping?(:picture).should be(false) + @landscape.picture_crop_x = 0.0 + @landscape.picture_crop_y = 0.0 + @landscape.picture_crop_w = 400 + @landscape.picture_crop_h = 300 + @landscape.cropping?(:picture).should be(true) + end + + + it "crops images" do + @landscape.picture_crop_x = 300.0 + @landscape.picture_crop_y = 200.0 + @landscape.picture_crop_w = 400 + @landscape.picture_crop_h = 300 + @landscape.save + + compare_images(CROPPED_IMG_PATH, @landscape.picture.path(:medium)).should eq(0.0) + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..da801c0 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,41 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +ENV["RAILS_ENV"] = 'test' +require File.expand_path("../../test_app/config/environment", __FILE__) +require 'rspec/rails' +require 'rspec/autorun' + +# Requires supporting ruby files with custom matchers and macros, etc, +# in spec/support/ and its subdirectories. +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} + +# Previously cropped image. Used for comparing test cropped images with RMagick +CROPPED_IMG_PATH = "test_app/test/fixtures/test_img.jpg" + +RSpec.configure do |config| + # ## Mock Framework + # + # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: + # + # config.mock_with :mocha + # config.mock_with :flexmock + # config.mock_with :rr + + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # If true, the base class of anonymous controllers will be inferred + # automatically. This will be the default behavior in future versions of + # rspec-rails. + config.infer_base_class_for_anonymous_controllers = false + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = "random" +end diff --git a/spec/support/compare_images.rb b/spec/support/compare_images.rb new file mode 100644 index 0000000..ca0d819 --- /dev/null +++ b/spec/support/compare_images.rb @@ -0,0 +1,8 @@ +require "RMagick" + +def compare_images(test_image_path, cropped_image_path) + test_img = Magick::Image::read(test_image_path).first + target_img = Magick::Image::read(cropped_image_path).first + + test_img.compare_channel(target_img, Magick::MeanAbsoluteErrorMetric).second +end \ No newline at end of file diff --git a/test_app/Gemfile b/test_app/Gemfile new file mode 100644 index 0000000..2aef071 --- /dev/null +++ b/test_app/Gemfile @@ -0,0 +1,17 @@ +source 'https://rubygems.org' + +gem 'rails', '3.2.8' + +gem 'jquery-rails' +gem 'execjs' +gem 'therubyracer' +gem 'sqlite3' +gem 'paperclip' + +gem 'papercrop', :path => ".." + +group :assets do + gem 'sass-rails', '~> 3.2.3' + gem 'coffee-rails', '~> 3.2.1' + gem 'uglifier', '>= 1.0.3' +end diff --git a/test_app/Rakefile b/test_app/Rakefile new file mode 100644 index 0000000..9946cea --- /dev/null +++ b/test_app/Rakefile @@ -0,0 +1,7 @@ +#!/usr/bin/env rake +# Add your own tasks in files placed in lib/tasks ending in .rake, +# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. + +require File.expand_path('../config/application', __FILE__) + +TestApp::Application.load_tasks diff --git a/test_app/app/assets/images/.gitkeep b/test_app/app/assets/images/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test_app/app/assets/javascripts/application.js b/test_app/app/assets/javascripts/application.js new file mode 100644 index 0000000..a4662bd --- /dev/null +++ b/test_app/app/assets/javascripts/application.js @@ -0,0 +1,17 @@ +// This is a manifest file that'll be compiled into application.js, which will include all the files +// listed below. +// +// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, +// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. +// +// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the +// the compiled file. +// +// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD +// GO AFTER THE REQUIRES BELOW. +// +//= require jquery +//= require jquery_ujs +//= require jquery.jcrop +//= require papercrop +//= require_tree . diff --git a/test_app/app/assets/stylesheets/application.css b/test_app/app/assets/stylesheets/application.css new file mode 100644 index 0000000..16c3175 --- /dev/null +++ b/test_app/app/assets/stylesheets/application.css @@ -0,0 +1,14 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, + * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the top of the + * compiled file, but it's generally better to create a new file per style scope. + * + *= require_self + *= require jquery.jcrop + *= require_tree . + */ diff --git a/test_app/app/assets/stylesheets/landscapes.css.scss b/test_app/app/assets/stylesheets/landscapes.css.scss new file mode 100644 index 0000000..8b28208 --- /dev/null +++ b/test_app/app/assets/stylesheets/landscapes.css.scss @@ -0,0 +1,14 @@ +.landscape-crop-preview { + float : left; + margin-right : 20px; + margin-bottom : 20px; +} + +.landscape-crop-area { + float : left; + margin-bottom : 20px; +} + +.clear { + clear : both; +} \ No newline at end of file diff --git a/test_app/app/assets/stylesheets/scaffolds.css.scss b/test_app/app/assets/stylesheets/scaffolds.css.scss new file mode 100644 index 0000000..6ec6a8f --- /dev/null +++ b/test_app/app/assets/stylesheets/scaffolds.css.scss @@ -0,0 +1,69 @@ +body { + background-color: #fff; + color: #333; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + &:visited { + color: #666; + } + &:hover { + color: #fff; + background-color: #000; + } +} + +div { + &.field, &.actions { + margin-bottom: 10px; + } +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0px; + background-color: #c00; + color: #fff; + } + ul li { + font-size: 12px; + list-style: square; + } +} diff --git a/test_app/app/controllers/application_controller.rb b/test_app/app/controllers/application_controller.rb new file mode 100644 index 0000000..e8065d9 --- /dev/null +++ b/test_app/app/controllers/application_controller.rb @@ -0,0 +1,3 @@ +class ApplicationController < ActionController::Base + protect_from_forgery +end diff --git a/test_app/app/controllers/landscapes_controller.rb b/test_app/app/controllers/landscapes_controller.rb new file mode 100644 index 0000000..b3e6445 --- /dev/null +++ b/test_app/app/controllers/landscapes_controller.rb @@ -0,0 +1,83 @@ +class LandscapesController < ApplicationController + # GET /landscapes + # GET /landscapes.json + def index + @landscapes = Landscape.all + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @landscapes } + end + end + + # GET /landscapes/1 + # GET /landscapes/1.json + def show + @landscape = Landscape.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @landscape } + end + end + + # GET /landscapes/new + # GET /landscapes/new.json + def new + @landscape = Landscape.new + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @landscape } + end + end + + # GET /landscapes/1/edit + def edit + @landscape = Landscape.find(params[:id]) + end + + # POST /landscapes + # POST /landscapes.json + def create + @landscape = Landscape.new(params[:landscape]) + + respond_to do |format| + if @landscape.save + format.html { render 'crop' } + format.json { render json: @landscape, status: :created, location: @landscape } + else + format.html { render action: "new" } + format.json { render json: @landscape.errors, status: :unprocessable_entity } + end + end + end + + # PUT /landscapes/1 + # PUT /landscapes/1.json + def update + @landscape = Landscape.find(params[:id]) + + respond_to do |format| + if @landscape.update_attributes(params[:landscape]) + format.html { redirect_to @landscape, notice: 'Landscape was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @landscape.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /landscapes/1 + # DELETE /landscapes/1.json + def destroy + @landscape = Landscape.find(params[:id]) + @landscape.destroy + + respond_to do |format| + format.html { redirect_to landscapes_url } + format.json { head :no_content } + end + end +end diff --git a/test_app/app/helpers/application_helper.rb b/test_app/app/helpers/application_helper.rb new file mode 100644 index 0000000..de6be79 --- /dev/null +++ b/test_app/app/helpers/application_helper.rb @@ -0,0 +1,2 @@ +module ApplicationHelper +end diff --git a/test_app/app/models/landscape.rb b/test_app/app/models/landscape.rb new file mode 100644 index 0000000..c05338d --- /dev/null +++ b/test_app/app/models/landscape.rb @@ -0,0 +1,8 @@ +class Landscape < ActiveRecord::Base + attr_protected + + has_attached_file :picture, :styles => {:thumb => "40x30", :medium => "400x300"}, + :path => ":rails_root/public/assets/uploads/landscapes/:id_:style.:extension", + :url => "uploads/landscapes/:id_:style.:extension" + crop_attached_file :picture, :aspect => "4:3" +end diff --git a/test_app/app/views/landscapes/_form.html.erb b/test_app/app/views/landscapes/_form.html.erb new file mode 100644 index 0000000..b7c167f --- /dev/null +++ b/test_app/app/views/landscapes/_form.html.erb @@ -0,0 +1,25 @@ +<%= form_for(@landscape) do |f| %> + <% if @landscape.errors.any? %> +
+

<%= pluralize(@landscape.errors.count, "error") %> prohibited this landscape from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
+ <%= f.label :picture %>
+ <%= f.file_field :picture %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/test_app/app/views/landscapes/crop.html.erb b/test_app/app/views/landscapes/crop.html.erb new file mode 100644 index 0000000..5b641ff --- /dev/null +++ b/test_app/app/views/landscapes/crop.html.erb @@ -0,0 +1,16 @@ +<%= form_for(@landscape) do |f| %> + +
+ <% # Will show a preview box with width=100, height is calculated by the aspect ratio. see model Landscape %> + <%= f.crop_preview :picture %> +
+
+ <% # Crop area. Mantains original image proportions. Crop area takes the aspect ratio %> + <%= f.cropbox :picture, :width => 600 %> +
+
+ +
+ <%= f.submit 'Crop image' %> +
+<% end %> \ No newline at end of file diff --git a/test_app/app/views/landscapes/edit.html.erb b/test_app/app/views/landscapes/edit.html.erb new file mode 100644 index 0000000..30e6da3 --- /dev/null +++ b/test_app/app/views/landscapes/edit.html.erb @@ -0,0 +1,6 @@ +

Editing landscape

+ +<%= render 'form' %> + +<%= link_to 'Show', @landscape %> | +<%= link_to 'Back', landscapes_path %> diff --git a/test_app/app/views/landscapes/index.html.erb b/test_app/app/views/landscapes/index.html.erb new file mode 100644 index 0000000..6f9aa69 --- /dev/null +++ b/test_app/app/views/landscapes/index.html.erb @@ -0,0 +1,25 @@ +

Listing landscapes

+ + + + + + + + + + +<% @landscapes.each do |landscape| %> + + + + + + + +<% end %> +
PictureName
<%= image_tag landscape.picture(:thumb) %><%= landscape.name %><%= link_to 'Show', landscape %><%= link_to 'Edit', edit_landscape_path(landscape) %><%= link_to 'Destroy', landscape, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Landscape', new_landscape_path %> diff --git a/test_app/app/views/landscapes/new.html.erb b/test_app/app/views/landscapes/new.html.erb new file mode 100644 index 0000000..72d7b46 --- /dev/null +++ b/test_app/app/views/landscapes/new.html.erb @@ -0,0 +1,5 @@ +

New landscape

+ +<%= render 'form' %> + +<%= link_to 'Back', landscapes_path %> diff --git a/test_app/app/views/landscapes/show.html.erb b/test_app/app/views/landscapes/show.html.erb new file mode 100644 index 0000000..920c9af --- /dev/null +++ b/test_app/app/views/landscapes/show.html.erb @@ -0,0 +1,13 @@ +

<%= notice %>

+ +

+ Name: + <%= @landscape.name %> +

+ +<%= image_tag @landscape.picture(:medium) %> + +
+ +<%= link_to 'Edit', edit_landscape_path(@landscape) %> | +<%= link_to 'Back', landscapes_path %> diff --git a/test_app/app/views/layouts/application.html.erb b/test_app/app/views/layouts/application.html.erb new file mode 100644 index 0000000..d4557d8 --- /dev/null +++ b/test_app/app/views/layouts/application.html.erb @@ -0,0 +1,14 @@ + + + + TestApp + <%= stylesheet_link_tag "application", :media => "all" %> + <%= javascript_include_tag "application" %> + <%= csrf_meta_tags %> + + + +<%= yield %> + + + diff --git a/test_app/config.ru b/test_app/config.ru new file mode 100644 index 0000000..86a587d --- /dev/null +++ b/test_app/config.ru @@ -0,0 +1,4 @@ +# This file is used by Rack-based servers to start the application. + +require ::File.expand_path('../config/environment', __FILE__) +run TestApp::Application diff --git a/test_app/config/application.rb b/test_app/config/application.rb new file mode 100644 index 0000000..a4761b8 --- /dev/null +++ b/test_app/config/application.rb @@ -0,0 +1,65 @@ +require File.expand_path('../boot', __FILE__) + +require 'rails/all' + +if defined?(Bundler) + # If you precompile assets before deploying to production, use this line + Bundler.require(*Rails.groups(:assets => %w(development test))) + # If you want your assets lazily compiled in production, use this line + # Bundler.require(:default, :assets, Rails.env) +end + +# Ensuring jquery presence on tests +require "jquery-rails" + +module TestApp + class Application < Rails::Application + # Settings in config/environments/* take precedence over those specified here. + # Application configuration should go into files in config/initializers + # -- all .rb files in that directory are automatically loaded. + + # Custom directories with classes and modules you want to be autoloadable. + # config.autoload_paths += %W(#{config.root}/extras) + + # Only load the plugins named here, in the order given (default is alphabetical). + # :all can be used as a placeholder for all plugins not explicitly named. + # config.plugins = [ :exception_notification, :ssl_requirement, :all ] + + # Activate observers that should always be running. + # config.active_record.observers = :cacher, :garbage_collector, :forum_observer + + # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. + # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. + # config.time_zone = 'Central Time (US & Canada)' + + # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. + # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] + # config.i18n.default_locale = :de + + # Configure the default encoding used in templates for Ruby 1.9. + config.encoding = "utf-8" + + # Configure sensitive parameters which will be filtered from the log file. + config.filter_parameters += [:password] + + # Enable escaping HTML in JSON. + config.active_support.escape_html_entities_in_json = true + + # Use SQL instead of Active Record's schema dumper when creating the database. + # This is necessary if your schema can't be completely dumped by the schema dumper, + # like if you have constraints or database-specific column types + # config.active_record.schema_format = :sql + + # Enforce whitelist mode for mass assignment. + # This will create an empty whitelist of attributes available for mass-assignment for all models + # in your app. As such, your models will need to explicitly whitelist or blacklist accessible + # parameters by using an attr_accessible or attr_protected declaration. + config.active_record.whitelist_attributes = false + + # Enable the asset pipeline + config.assets.enabled = true + + # Version of your assets, change this if you want to expire all your assets + config.assets.version = '1.0' + end +end diff --git a/test_app/config/boot.rb b/test_app/config/boot.rb new file mode 100644 index 0000000..4489e58 --- /dev/null +++ b/test_app/config/boot.rb @@ -0,0 +1,6 @@ +require 'rubygems' + +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) + +require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) diff --git a/test_app/config/database.yml b/test_app/config/database.yml new file mode 100644 index 0000000..5002692 --- /dev/null +++ b/test_app/config/database.yml @@ -0,0 +1,17 @@ +development: + adapter: sqlite3 + database: db/development.sqlite3 + pool: 5 + timeout: 5000 + +test: + adapter: sqlite3 + database: db/test.sqlite3 + pool: 5 + timeout: 5000 + +production: + adapter: sqlite3 + database: db/production.sqlite3 + pool: 5 + timeout: 5000 diff --git a/test_app/config/environment.rb b/test_app/config/environment.rb new file mode 100644 index 0000000..f4cc1e4 --- /dev/null +++ b/test_app/config/environment.rb @@ -0,0 +1,5 @@ +# Load the rails application +require File.expand_path('../application', __FILE__) + +# Initialize the rails application +TestApp::Application.initialize! diff --git a/test_app/config/environments/development.rb b/test_app/config/environments/development.rb new file mode 100644 index 0000000..b5306b3 --- /dev/null +++ b/test_app/config/environments/development.rb @@ -0,0 +1,37 @@ +TestApp::Application.configure do + # Settings specified here will take precedence over those in config/application.rb + + # In the development environment your application's code is reloaded on + # every request. This slows down response time but is perfect for development + # since you don't have to restart the web server when you make code changes. + config.cache_classes = false + + # Log error messages when you accidentally call methods on nil. + config.whiny_nils = true + + # Show full error reports and disable caching + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + + # Don't care if the mailer can't send + config.action_mailer.raise_delivery_errors = false + + # Print deprecation notices to the Rails logger + config.active_support.deprecation = :log + + # Only use best-standards-support built into browsers + config.action_dispatch.best_standards_support = :builtin + + # Raise exception on mass assignment protection for Active Record models + config.active_record.mass_assignment_sanitizer = :strict + + # Log the query plan for queries taking more than this (works + # with SQLite, MySQL, and PostgreSQL) + config.active_record.auto_explain_threshold_in_seconds = 0.5 + + # Do not compress assets + config.assets.compress = false + + # Expands the lines which load the assets + config.assets.debug = true +end diff --git a/test_app/config/environments/production.rb b/test_app/config/environments/production.rb new file mode 100644 index 0000000..50fcca3 --- /dev/null +++ b/test_app/config/environments/production.rb @@ -0,0 +1,67 @@ +TestApp::Application.configure do + # Settings specified here will take precedence over those in config/application.rb + + # Code is not reloaded between requests + config.cache_classes = true + + # Full error reports are disabled and caching is turned on + config.consider_all_requests_local = false + config.action_controller.perform_caching = true + + # Disable Rails's static asset server (Apache or nginx will already do this) + config.serve_static_assets = false + + # Compress JavaScripts and CSS + config.assets.compress = true + + # Don't fallback to assets pipeline if a precompiled asset is missed + config.assets.compile = false + + # Generate digests for assets URLs + config.assets.digest = true + + # Defaults to nil and saved in location specified by config.assets.prefix + # config.assets.manifest = YOUR_PATH + + # Specifies the header that your server uses for sending files + # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache + # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx + + # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. + # config.force_ssl = true + + # See everything in the log (default is :info) + # config.log_level = :debug + + # Prepend all log lines with the following tags + # config.log_tags = [ :subdomain, :uuid ] + + # Use a different logger for distributed setups + # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) + + # Use a different cache store in production + # config.cache_store = :mem_cache_store + + # Enable serving of images, stylesheets, and JavaScripts from an asset server + # config.action_controller.asset_host = "http://assets.example.com" + + # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) + # config.assets.precompile += %w( search.js ) + + # Disable delivery errors, bad email addresses will be ignored + # config.action_mailer.raise_delivery_errors = false + + # Enable threaded mode + # config.threadsafe! + + # Enable locale fallbacks for I18n (makes lookups for any locale fall back to + # the I18n.default_locale when a translation can not be found) + config.i18n.fallbacks = true + + # Send deprecation notices to registered listeners + config.active_support.deprecation = :notify + + # Log the query plan for queries taking more than this (works + # with SQLite, MySQL, and PostgreSQL) + # config.active_record.auto_explain_threshold_in_seconds = 0.5 +end diff --git a/test_app/config/environments/test.rb b/test_app/config/environments/test.rb new file mode 100644 index 0000000..21ec8e4 --- /dev/null +++ b/test_app/config/environments/test.rb @@ -0,0 +1,37 @@ +TestApp::Application.configure do + # Settings specified here will take precedence over those in config/application.rb + + # The test environment is used exclusively to run your application's + # test suite. You never need to work with it otherwise. Remember that + # your test database is "scratch space" for the test suite and is wiped + # and recreated between test runs. Don't rely on the data there! + config.cache_classes = true + + # Configure static asset server for tests with Cache-Control for performance + config.serve_static_assets = true + config.static_cache_control = "public, max-age=3600" + + # Log error messages when you accidentally call methods on nil + config.whiny_nils = true + + # Show full error reports and disable caching + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + + # Raise exceptions instead of rendering exception templates + config.action_dispatch.show_exceptions = false + + # Disable request forgery protection in test environment + config.action_controller.allow_forgery_protection = false + + # Tell Action Mailer not to deliver emails to the real world. + # The :test delivery method accumulates sent emails in the + # ActionMailer::Base.deliveries array. + config.action_mailer.delivery_method = :test + + # Raise exception on mass assignment protection for Active Record models + config.active_record.mass_assignment_sanitizer = :strict + + # Print deprecation notices to the stderr + config.active_support.deprecation = :stderr +end diff --git a/test_app/config/initializers/backtrace_silencers.rb b/test_app/config/initializers/backtrace_silencers.rb new file mode 100644 index 0000000..59385cd --- /dev/null +++ b/test_app/config/initializers/backtrace_silencers.rb @@ -0,0 +1,7 @@ +# Be sure to restart your server when you modify this file. + +# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. +# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } + +# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. +# Rails.backtrace_cleaner.remove_silencers! diff --git a/test_app/config/initializers/inflections.rb b/test_app/config/initializers/inflections.rb new file mode 100644 index 0000000..5d8d9be --- /dev/null +++ b/test_app/config/initializers/inflections.rb @@ -0,0 +1,15 @@ +# Be sure to restart your server when you modify this file. + +# Add new inflection rules using the following format +# (all these examples are active by default): +# ActiveSupport::Inflector.inflections do |inflect| +# inflect.plural /^(ox)$/i, '\1en' +# inflect.singular /^(ox)en/i, '\1' +# inflect.irregular 'person', 'people' +# inflect.uncountable %w( fish sheep ) +# end +# +# These inflection rules are supported but not enabled by default: +# ActiveSupport::Inflector.inflections do |inflect| +# inflect.acronym 'RESTful' +# end diff --git a/test_app/config/initializers/mime_types.rb b/test_app/config/initializers/mime_types.rb new file mode 100644 index 0000000..72aca7e --- /dev/null +++ b/test_app/config/initializers/mime_types.rb @@ -0,0 +1,5 @@ +# Be sure to restart your server when you modify this file. + +# Add new mime types for use in respond_to blocks: +# Mime::Type.register "text/richtext", :rtf +# Mime::Type.register_alias "text/html", :iphone diff --git a/test_app/config/initializers/secret_token.rb b/test_app/config/initializers/secret_token.rb new file mode 100644 index 0000000..6b315a5 --- /dev/null +++ b/test_app/config/initializers/secret_token.rb @@ -0,0 +1,7 @@ +# Be sure to restart your server when you modify this file. + +# Your secret key for verifying the integrity of signed cookies. +# If you change this key, all old signed cookies will become invalid! +# Make sure the secret is at least 30 characters and all random, +# no regular words or you'll be exposed to dictionary attacks. +TestApp::Application.config.secret_token = 'eae79e907953926607428fa84311c5ed657c8738ef011d82ebeebe1fee8b0bbe19deae58263814bd7bb14e4b97bbf204f92b95f4a7c6dc97aa36f91ccbec45ac' diff --git a/test_app/config/initializers/session_store.rb b/test_app/config/initializers/session_store.rb new file mode 100644 index 0000000..4520d03 --- /dev/null +++ b/test_app/config/initializers/session_store.rb @@ -0,0 +1,8 @@ +# Be sure to restart your server when you modify this file. + +TestApp::Application.config.session_store :cookie_store, key: '_test_app_session' + +# Use the database for sessions instead of the cookie-based default, +# which shouldn't be used to store highly confidential information +# (create the session table with "rails generate session_migration") +# TestApp::Application.config.session_store :active_record_store diff --git a/test_app/config/initializers/wrap_parameters.rb b/test_app/config/initializers/wrap_parameters.rb new file mode 100644 index 0000000..999df20 --- /dev/null +++ b/test_app/config/initializers/wrap_parameters.rb @@ -0,0 +1,14 @@ +# Be sure to restart your server when you modify this file. +# +# This file contains settings for ActionController::ParamsWrapper which +# is enabled by default. + +# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. +ActiveSupport.on_load(:action_controller) do + wrap_parameters format: [:json] +end + +# Disable root element in JSON by default. +ActiveSupport.on_load(:active_record) do + self.include_root_in_json = false +end diff --git a/test_app/config/locales/en.yml b/test_app/config/locales/en.yml new file mode 100644 index 0000000..179c14c --- /dev/null +++ b/test_app/config/locales/en.yml @@ -0,0 +1,5 @@ +# Sample localization file for English. Add more files in this directory for other locales. +# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points. + +en: + hello: "Hello world" diff --git a/test_app/config/routes.rb b/test_app/config/routes.rb new file mode 100644 index 0000000..98a9382 --- /dev/null +++ b/test_app/config/routes.rb @@ -0,0 +1,5 @@ +TestApp::Application.routes.draw do + resources :landscapes + + root :to => 'landscapes#index' +end diff --git a/test_app/db/migrate/20120814094634_create_landscapes.rb b/test_app/db/migrate/20120814094634_create_landscapes.rb new file mode 100644 index 0000000..e51414b --- /dev/null +++ b/test_app/db/migrate/20120814094634_create_landscapes.rb @@ -0,0 +1,13 @@ +class CreateLandscapes < ActiveRecord::Migration + def change + create_table :landscapes do |t| + t.string :name + t.string :picture_file_name + t.string :picture_content_type + t.integer :picture_file_size + t.datetime :picture_updated_at + + t.timestamps + end + end +end diff --git a/test_app/db/schema.rb b/test_app/db/schema.rb new file mode 100644 index 0000000..3320ef2 --- /dev/null +++ b/test_app/db/schema.rb @@ -0,0 +1,26 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended to check this file into your version control system. + +ActiveRecord::Schema.define(:version => 20120814094634) do + + create_table "landscapes", :force => true do |t| + t.string "name" + t.string "picture_file_name" + t.string "picture_content_type" + t.integer "picture_file_size" + t.datetime "picture_updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + +end diff --git a/test_app/db/seeds.rb b/test_app/db/seeds.rb new file mode 100644 index 0000000..4edb1e8 --- /dev/null +++ b/test_app/db/seeds.rb @@ -0,0 +1,7 @@ +# This file should contain all the record creation needed to seed the database with its default values. +# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). +# +# Examples: +# +# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) +# Mayor.create(name: 'Emanuel', city: cities.first) diff --git a/test_app/lib/assets/.gitkeep b/test_app/lib/assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test_app/lib/tasks/.gitkeep b/test_app/lib/tasks/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test_app/log/.gitkeep b/test_app/log/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test_app/public/404.html b/test_app/public/404.html new file mode 100644 index 0000000..9a48320 --- /dev/null +++ b/test_app/public/404.html @@ -0,0 +1,26 @@ + + + + The page you were looking for doesn't exist (404) + + + + + +
+

The page you were looking for doesn't exist.

+

You may have mistyped the address or the page may have moved.

+
+ + diff --git a/test_app/public/422.html b/test_app/public/422.html new file mode 100644 index 0000000..83660ab --- /dev/null +++ b/test_app/public/422.html @@ -0,0 +1,26 @@ + + + + The change you wanted was rejected (422) + + + + + +
+

The change you wanted was rejected.

+

Maybe you tried to change something you didn't have access to.

+
+ + diff --git a/test_app/public/500.html b/test_app/public/500.html new file mode 100644 index 0000000..f3648a0 --- /dev/null +++ b/test_app/public/500.html @@ -0,0 +1,25 @@ + + + + We're sorry, but something went wrong (500) + + + + + +
+

We're sorry, but something went wrong.

+
+ + diff --git a/test_app/public/favicon.ico b/test_app/public/favicon.ico new file mode 100644 index 0000000..e69de29 diff --git a/test_app/script/rails b/test_app/script/rails new file mode 100644 index 0000000..f8da2cf --- /dev/null +++ b/test_app/script/rails @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby +# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. + +APP_PATH = File.expand_path('../../config/application', __FILE__) +require File.expand_path('../../config/boot', __FILE__) +require 'rails/commands' diff --git a/test_app/test/fixtures/matterhorn.jpg b/test_app/test/fixtures/matterhorn.jpg new file mode 100644 index 0000000..83a4bc2 Binary files /dev/null and b/test_app/test/fixtures/matterhorn.jpg differ diff --git a/test_app/test/fixtures/test_img.jpg b/test_app/test/fixtures/test_img.jpg new file mode 100644 index 0000000..0b7d900 Binary files /dev/null and b/test_app/test/fixtures/test_img.jpg differ diff --git a/test_app/test/functional/.gitkeep b/test_app/test/functional/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test_app/test/functional/landscapes_controller_test.rb b/test_app/test/functional/landscapes_controller_test.rb new file mode 100644 index 0000000..6a513fa --- /dev/null +++ b/test_app/test/functional/landscapes_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class LandscapesControllerTest < ActionController::TestCase + setup do + @landscape = landscapes(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:landscapes) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create landscape" do + assert_difference('Landscape.count') do + post :create, landscape: { name: @landscape.name } + end + + assert_redirected_to landscape_path(assigns(:landscape)) + end + + test "should show landscape" do + get :show, id: @landscape + assert_response :success + end + + test "should get edit" do + get :edit, id: @landscape + assert_response :success + end + + test "should update landscape" do + put :update, id: @landscape, landscape: { name: @landscape.name } + assert_redirected_to landscape_path(assigns(:landscape)) + end + + test "should destroy landscape" do + assert_difference('Landscape.count', -1) do + delete :destroy, id: @landscape + end + + assert_redirected_to landscapes_path + end +end diff --git a/test_app/test/integration/.gitkeep b/test_app/test/integration/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test_app/test/performance/browsing_test.rb b/test_app/test/performance/browsing_test.rb new file mode 100644 index 0000000..3fea27b --- /dev/null +++ b/test_app/test/performance/browsing_test.rb @@ -0,0 +1,12 @@ +require 'test_helper' +require 'rails/performance_test_help' + +class BrowsingTest < ActionDispatch::PerformanceTest + # Refer to the documentation for all available options + # self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory] + # :output => 'tmp/performance', :formats => [:flat] } + + def test_homepage + get '/' + end +end diff --git a/test_app/test/test_helper.rb b/test_app/test/test_helper.rb new file mode 100644 index 0000000..8bf1192 --- /dev/null +++ b/test_app/test/test_helper.rb @@ -0,0 +1,13 @@ +ENV["RAILS_ENV"] = "test" +require File.expand_path('../../config/environment', __FILE__) +require 'rails/test_help' + +class ActiveSupport::TestCase + # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. + # + # Note: You'll currently still have to declare fixtures explicitly in integration tests + # -- they do not yet inherit this setting + fixtures :all + + # Add more helper methods to be used by all tests here... +end diff --git a/test_app/test/unit/.gitkeep b/test_app/test/unit/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test_app/test/unit/landscape_test.rb b/test_app/test/unit/landscape_test.rb new file mode 100644 index 0000000..1685448 --- /dev/null +++ b/test_app/test/unit/landscape_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class LandscapeTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test_app/vendor/assets/javascripts/.gitkeep b/test_app/vendor/assets/javascripts/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test_app/vendor/assets/stylesheets/.gitkeep b/test_app/vendor/assets/stylesheets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test_app/vendor/plugins/.gitkeep b/test_app/vendor/plugins/.gitkeep new file mode 100644 index 0000000..e69de29