forked from trailblazer/roar-rails
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ValidationsRepresenter to jsonify and deserialize ActiveModel v…
…alidations.
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'representable/json/collection' | ||
require 'representable/json/hash' | ||
|
||
# Represents a validators hash for a model. | ||
module ValidatorsRepresenter | ||
class ValidatorClient | ||
attr_accessor :kind, :options | ||
end | ||
|
||
# Represents a single Validator instance. | ||
module ValidatorRepresenter | ||
include Roar::Representer::JSON | ||
property :kind | ||
hash :options | ||
end | ||
|
||
# Represents an array of validators for an attribute. | ||
module AttributeValidators | ||
include Representable::JSON::Collection | ||
items :extend => ValidatorRepresenter, :class => ValidatorClient | ||
end | ||
|
||
include Representable::JSON::Hash | ||
values :extend => AttributeValidators, :class => Array | ||
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,34 @@ | ||
require 'test_helper' | ||
require 'active_model' | ||
require 'roar/rails/validations_representer' | ||
|
||
|
||
class ValidationsTest < MiniTest::Spec | ||
class Comment | ||
include ActiveModel::Validations | ||
validates :name, :presence => true | ||
end | ||
|
||
describe "ValidatorRepresenter" do | ||
it "renders a representation in #to_json" do | ||
assert_equal "{\"kind\":\"presence\",\"options\":{}}", Comment._validators[:name].first.extend(ValidatorsRepresenter::ValidatorRepresenter).to_json | ||
end | ||
|
||
it "parses validator config in #from_json" do | ||
validator = ValidatorsRepresenter::ValidatorClient.new.extend(ValidatorsRepresenter::ValidatorRepresenter).from_json("{\"kind\":\"presence\",\"attributes\":[\"name\"],\"options\":{}}") | ||
assert_equal "presence", validator.kind | ||
assert_equal({}, validator.options) | ||
end | ||
end | ||
|
||
describe "ValidatorsRepresenter" do | ||
it "renders a collection in #to_json" do | ||
assert_equal "{\"name\":[{\"kind\":\"presence\",\"options\":{}}]}", Comment._validators.extend(ValidatorsRepresenter).to_json | ||
end | ||
|
||
it "parses validators in #from_json" do | ||
validations = {}.extend(ValidatorsRepresenter).from_json("{\"name\":[{\"kind\":\"presence\",\"options\":[]}]}") | ||
assert_equal "presence", validations["name"].first.kind | ||
end | ||
end | ||
end |