Giter Site home page Giter Site logo

andersondias / attributes_sanitizer Goto Github PK

View Code? Open in Web Editor NEW
23.0 1.0 2.0 94 KB

A simple way to append sanitizers to attributes to Rails 5+ applications.

License: MIT License

Ruby 85.44% JavaScript 2.30% CSS 1.43% HTML 10.83%
ruby ruby-on-rails rubygems rails gem gems rails-plugins

attributes_sanitizer's Introduction

Maintainability Test Coverage Build

AttributesSanitizer

A simple way to append sanitizers to attributes on Rails.

Usage

class Product < ApplicationRecord
  sanitize_attribute :title, with: -> (value) {
    value.gsub(/[1-9]/, 'X')
  }

  sanitize_attributes :title, :description, with: [:downcase, :strip_tags]
end

It comes with pre-defined sanitizers:

  • :stringify which perform a to_s into the value, to be sanitized as a string. Can be used before other sanitizer, that depends to the value be a string
  • :downcase which downcases a given attribute string
  • :upcase which upcases a given attribute string
  • :strip_tags which removes any tags from the given string based on Rails sanitize helper.
  • :strip_emojis which removes any emoji from the given string
  • :strip_spaces which removes any white spaces from the beginning and end of given attribute

You might define your own sanitizers:

# config/initializers/attribute_sanitizers.rb

AttributesSanitizer.define_sanitizer :reverse do |value|
  value.to_s.reverse
end

It also comes with predefined bundles:

class Product < ApplicationRecord
  sanitize_attribute :title, with: -> (value) {
    value.gsub(/[1-9]/, 'X')
  }

  sanitize_attributes :title, :description, with: :no_tags_emojis_or_extra_spaces
  # same as: `with: %i(stringify strip_tags strip_emojis strip_spaces)
end

And, finally, you are able to define your own bundles:

# config/initializers/attribute_sanitizers.rb
AttributesSanitizer.define_bundle(:my_bundle, %i(downcase strip_spaces))

Installation

Add this line to your application's Gemfile:

gem 'attributes_sanitizer'

And then execute:

$ bundle

Or install it yourself as:

$ gem install attributes_sanitizer

License

The gem is available as open source under the terms of the MIT License.

attributes_sanitizer's People

Contributors

andersondias avatar andreleoni avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

andreleoni dmkl

attributes_sanitizer's Issues

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.