Git Product home page Git Product logo

paperclip-dimension-validator's Introduction

paperclip-dimension-validator

Validate them dimensions!

Installation

Add this line to your application's Gemfile:

gem 'paperclip-dimension-validator'

And then execute:

$ bundle

Or install it yourself as:

$ gem install paperclip-dimension-validator

Usage

This gem introduces the dimensions validator for Paperclip's validates_attachment. dimensions accepts a hash of height and width integer pixel values.

Example

class Image < ActiveRecord::Base
  has_attached_file :avatar

  # Enforce exact width/height
  validates_attachment :avatar, dimensions: { height: 30, width: 30 }
end

Example 2

class Image < ActiveRecord::Base
  has_attached_file :avatar

  # Enforce min/max width/height
  validates_attachment :avatar, dimensions: { min_height: 30, min_width: 30, max_height: 2000, max_width: 2000 }
end

Example 3

class Image < ActiveRecord::Base
  has_attached_file :avatar

  # Ensure (width x height) is between 100 & 10000
  validates_attachment :avatar, dimensions: { min_pixels: 100, max_pixels: 10000 }
end

Testing

paperclip-dimension-validator includes rspec-compatible matchers for testing.

Note In order to use these matchers make sure to include either chunky_png or oily_png as a dependency in your Gemfile.

Gemfile

group :test do
  gem 'chunky_png'
end

RSpec

In spec_helper.rb, you'll need to require the matchers:

require 'paperclip/matchers/validate_attachment_dimesions_matcher'

And include the paperclip matchers module:

RSpec.configure do |config|
  config.include Paperclip::Shoulda::Matchers
end

Example

it { should validate_attachment_dimensions(:avatar).height(30).width(30) }

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

paperclip-dimension-validator's People

Contributors

ktopping avatar sirrawlins avatar

Watchers

James Cloos avatar

Forkers

kinoute

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.