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
9 changed files
with
94 additions
and
15 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
require "acts_as_virtual_field/version" | ||
|
||
module ActsAsVirtualField | ||
require 'acts_as_virtual_field/virtual_field' | ||
end | ||
require 'acts_as_virtual_field/virtual_field' | ||
require "acts_as_virtual_field/formtastic/virtual_field_type_input" | ||
require "acts_as_virtual_field/formtastic/virtual_input" | ||
require "acts_as_virtual_field/active_record/acts/virtual_field" |
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
9 changes: 9 additions & 0 deletions
9
lib/acts_as_virtual_field/formtastic/virtual_field_type_input.rb
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,9 @@ | ||
module Formtastic | ||
module Inputs | ||
class VirtualFieldTypeInput < Formtastic::Inputs::SelectInput | ||
def collection_from_options | ||
::ActsAsVirtualField::VirtualField.types | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Formtastic | ||
module Helpers | ||
module InputHelper | ||
def virtual_input(method, options = {}) | ||
base_options = @object.formtastic_options | ||
input(method, options.merge(base_options)) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Fields | ||
module Validations | ||
autoload :Required, 'fields/validations/required' | ||
autoload :Formated, 'fields/validations/formated' | ||
autoload :Conditioned, 'fields/validations/conditioned' | ||
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,30 @@ | ||
module Fields | ||
module Validations | ||
module Formated | ||
|
||
def validates_field_value_format_of(*attr_names) | ||
validates_with FieldsFormatValidator, _merge_attributes(attr_names) | ||
end | ||
|
||
class FieldsFormatValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
normal_value = Yajl::Parser.parse(value || '') | ||
unless normal_value.empty? | ||
klass = "::Fields::#{record.field_type}".constantize | ||
begin | ||
args = case record.field_type | ||
when "SingleList", "MultipleList" | ||
[record.field_meta, value] | ||
else | ||
[normal_value] | ||
end | ||
klass.new(*args) | ||
rescue | ||
record.errors[:base] << (options[:message] || "has broken format (must be #{record.field_type})") | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Fields | ||
module Validations | ||
module Required | ||
|
||
def validates_field_value_presence_of(*attr_names) | ||
validates_with FieldsRequiredValidator, _merge_attributes(attr_names) | ||
end | ||
|
||
class FieldsRequiredValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
normal_value = Yajl::Parser.parse(value || '') | ||
if record.required? && normal_value.empty? | ||
record.errors[:base] << (options[:message] || "is required") | ||
end | ||
end | ||
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