Giter Club home page Giter Club logo

activecleaner's Introduction

ActiveCleaner

Gem Version Build Status

ActiveCleaner is a set of helpers that helps you in cleaning user-typed content in your ActiveModel depending models (ActiveRecord, Mongoid...)

Extra spaces mean extra storage. And it could ruin your indexes and your sortings.

Tired of doing everywhere:

before_validation :clean_title

def clean_title
  unless title.nil?
    self.title = title.squish
  end
  self.title = nil if title.blank?

  true
end

Cleaners included

  • :string (StringCleaner, the default one) : cleans all the space characters. It turns " A \n \t title \t " into "A title".
  • :text (TextCleaner) : like :string, but preserves new lines (with a max of 2 successive new lines). useful when the field is rendered with the simple_format Rails helper.
  • :markdown (MarkdownCleaner) : like :text, but preserves spaces in the beginning of lines (the indentation). useful for... markdown!
  • :utf8mb3 (Utf8mb3Cleaner) : removes all 4-bytes encoded chars in UTF8 strings that mess with the utf8 encoding in MySQL (iOS6 emojis for example).

Installation

Add the gem to your Gemfile:

gem 'activecleaner'

Or install with RubyGems:

$ gem install activecleaner

Usage

Basic usage

Add include ActiveCleaner in your model and also do:

clean :field_1, :field_2 ... :field_n, options_1: :value, options_2: :value

Options

  • :as (default is :string) : the symbol name of the cleaner.
  • :nilify (default is false) : set to nil when the field was or is cleaned to "".

Example

class Post
  include Mongoid::Document
  include ActiveCleaner

  field :title
  field :subtitle
  clean :title, :subtitle, nilify: true

  field :body
  clean :body, as: :text, nilify: true
end

Contributing

Contributions and bug reports are welcome.

Clone the repository and run bundle install to setup the development environment.

Provide a case spec according to your changes/needs, taking example on existing ones (in spec/cases).

To run the specs:

bundle exec rspec

You can also use guard to run the specs during dev.

Credits

License - Report a bug - Rubygems page

activecleaner's People

Contributors

andrewhao avatar maximeg avatar

Stargazers

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

Watchers

 avatar  avatar

activecleaner's Issues

New Feature: #clean_all

I typically clean and nilify all string and text columns on my model. Sometimes when I or a developer on my team adds additional fields, we forget to add it to the #clean method. So it'd be slick if we could have a #clean_all method that would automatically clean the fields of a certain type.

I created a Rails model concern to get this functionality in my app for now, but I thought it might be cool to have it built-in. Here's my concern:

# app/models/concerns/auto_cleaner.rb

module AutoCleaner
  extend ActiveSupport::Concern
  include ActiveCleaner

  included do
    clean columns_with_type(:string), as: :string, nilify: true
    clean columns_with_type(:text),   as: :text,   nilify: true
  end

  class_methods do
    def columns_with_type(type)
      columns.select { |c| c.type.to_sym == type.to_sym }.map(&:name).map(&:to_sym)
    end
  end
end

Proposed API:

class Post
  include ActiveCleaner

  clean_all :string, nilify: true
  clean_all :text, as: :text, nilify: true
end

I can create a PR with tests if this interests you. Lmk!

install fails on rails 4

I try to install the gem on my rails 4.1 app using ruby 2.1.1 and it fails. Its looking for rails 3 dependencies.

Bundler could not find compatible versions for gem "activesupport":
In Gemfile:
activecleaner (>= 0) ruby depends on
activesupport (> 3.1) ruby
rails (
> 4.1.0) ruby depends on
activesupport (4.1.0)
Process finished with exit code 6

Rails 5.2 release

I see this commit was added to test against Rails 5.2 9dd1e74 but no release was made. Is this gem ready for 5.2? Would it be possible to create a new release?

Cleaner expect that accessor is a string but do not typecast it and thus raises

With rails it is sometime difficult to control the type of the data when it is set, and the ORM fix it afterward and typecast it to the expected format.

Currently when a boolean access is received (and before he is typecasted to string) the cleaner is raising an error :

A NoMethodError occurred in #:
undefined method `each_char' for true:TrueClass
activecleaner (0.3.0) lib/active_cleaner/utf8mb3_cleaner.rb:8:in `clean_value'

I suppose that using to_s would fix the stuff, or handling it later in the ORM callback chain

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.