-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |