Skip to content

Commit

Permalink
add rails integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kelevro committed Oct 26, 2019
1 parent 55aad82 commit 1cfd43c
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
sudo: false
language: ruby
cache: bundler
gemfile:
- gemfiles/rails_4.2.10.gemfile
- gemfiles/rails_5.2.1.gemfile
- gemfiles/rails_6.0.0.gemfile
rvm:
- 2.4
- 2.5
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@

source "https://rubygems.org"

# gem "activemodel", "4.2.10"
# gem "activemodel", "5.2.1"
gem "activemodel", "6.0.0"

# Specify your gem's dependencies in attr_filters.gemspec
gemspec
17 changes: 17 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activemodel (6.0.0)
activesupport (= 6.0.0)
activesupport (6.0.0)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.1, >= 2.1.8)
ast (2.4.0)
concurrent-ruby (1.1.5)
diff-lcs (1.3)
i18n (1.7.0)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.3)
minitest (5.12.2)
parallel (1.18.0)
parser (2.6.5.0)
ast (~> 2.4.0)
Expand All @@ -35,12 +47,17 @@ GEM
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.7)
ruby-progressbar (1.10.1)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
unicode-display_width (1.6.0)
zeitwerk (2.2.0)

PLATFORMS
ruby

DEPENDENCIES
activemodel (= 6.0.0)
attr_filters!
bundler (~> 2.0)
rake (~> 10.0)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# AttrFilters

[![Build Status](https://travis-ci.com/Syndicode/attr-filters.svg?branch=master)](https://travis-ci.com/Syndicode/attr-filters)
[![Gem Version](https://badge.fury.io/rb/attr_filters.svg)](https://badge.fury.io/rb/attr_filters)

Light weight gem for filtering PORO attributes with zero dependencies.

Expand Down
5 changes: 5 additions & 0 deletions gemfiles/rails_4.2.10.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "activemodel", "4.2.10"
5 changes: 5 additions & 0 deletions gemfiles/rails_5.2.1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "activemodel", "5.2.1"
5 changes: 5 additions & 0 deletions gemfiles/rails_6.0.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "activemodel", "6.0.0"
16 changes: 16 additions & 0 deletions lib/active_model/validations/filters_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module ActiveModel
module Validations
class FiltersValidator < ActiveModel::EachValidator
def initialize(options)
filters = options.slice(*AttrFilters::Filters::LIST.keys)
model_class = options[:class]
model_class.filters(*options[:attributes], filters)
super(options)
end

def validate_each(_record, _attribute, _value); end
end
end
end
1 change: 1 addition & 0 deletions lib/attr_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class UnknownAttributeError < StandardError; end
autoload :FiltersMacro, "attr_filters/filters_macro"
autoload :InstanceMethods, "attr_filters/instance_methods"
autoload :Filters, "attr_filters/filters"
autoload :ActiveModel, "attr_filters/active_model"

def self.included(base)
base.extend(FiltersMacro)
Expand Down
22 changes: 22 additions & 0 deletions lib/attr_filters/active_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module AttrFilters
module ActiveModel
if AttrFilters::Utils.satisfied_spec?("activemodel")
require "active_model"
require "active_model/Validations/filters_validator"
end

def self.included(base)
base.include AttrFilters
base.extend Macro
base.init
end

module Macro
def init
before_validation :filter! if respond_to?(:before_validation)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/attr_filters/filters_macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def filters(*attributes)
end

def _registered_filters
@_registered_filters
@_registered_filters || {}
end

private
Expand Down
4 changes: 4 additions & 0 deletions lib/attr_filters/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ def self.extract_options(attrs)
options = last.is_a?(::Hash) ? last : {}
[attrs[0..-2], options]
end

def self.satisfied_spec?(spec_name)
Gem.loaded_specs.key?(spec_name)
end
end
end
74 changes: 74 additions & 0 deletions spec/attr_filters/active_model_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe AttrFilters::ActiveModel, if: active_model? do
context "integrate #filter! to Validations" do
it "should call #filter! method before validation" do
form_class = Struct.new(:user_name) do
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
include AttrFilters::ActiveModel

validates :user_name, presence: true
filters :user_name, trim: true
end

user = form_class.new("Mike Dou")

expect(user).to receive(:filter!)

user.valid?
end

it "should not call #filter! method before validation" do
form_class = Struct.new(:user_name) do
include ActiveModel::Validations
include AttrFilters::ActiveModel

validates :user_name, presence: true
filters :user_name, trim: true
end

user = form_class.new("Mike Dou")

expect(user).not_to receive(:filter!)

user.valid?
end
end

context "integrate filters into validates API" do
it "should call trim filter" do
form_class = Struct.new(:user_name) do
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
include AttrFilters::ActiveModel

validates :user_name, presence: true, filters: { trim: true }
end

user = form_class.new("Mike Dou")

expect(AttrFilters::Filters::LIST[:trim]).to receive(:call).with("Mike Dou")

user.valid?
end

it "should filter model attributes" do
form_class = Struct.new(:user_name) do
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
include AttrFilters::ActiveModel

validates :user_name, presence: true, filters: { trim: true, squeeze: true }
end

user = form_class.new(" Mike Dou ")

user.valid?

expect(user.user_name).to eq("Mike Dou")
end
end
end
13 changes: 13 additions & 0 deletions spec/attr_filters_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# frozen_string_literal: true

RSpec.describe AttrFilters do
context "filters not added" do
it "should do nothing" do
form_class = Struct.new(:user_name) do
include AttrFilters
end
form = form_class.new("Mike Dou")

form.filter!

expect(form.user_name).to eq("Mike Dou")
end
end

context "trim filter" do
it "should call trim filter" do
form_class = Struct.new(:user_name) do
Expand Down
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
require "bundler/setup"
require "attr_filters"

require_relative "./support/utils"
require_relative "./support/active_model/name" if active_model?

RSpec.configure do |config|
config.example_status_persistence_file_path = ".rspec_status"

Expand Down
13 changes: 13 additions & 0 deletions spec/support/active_model/name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

FakeModelKlass = Class.new

require "active_model"

module ActiveModel
class Name
def initialize(_klass, _namespace = nil, _name = nil)
@name = FakeModelKlass
end
end
end
5 changes: 5 additions & 0 deletions spec/support/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

def active_model?
AttrFilters::Utils.satisfied_spec?("activemodel")
end

0 comments on commit 1cfd43c

Please sign in to comment.