Class: PassionView::Form::Base
- Inherits:
-
ViewModel::Base
- Object
- ViewModel::Base
- PassionView::Form::Base
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations
- Defined in:
- lib/passion_view/form/base.rb
Defined Under Namespace
Classes: Delegation
Class Method Summary collapse
- .delegate(*methods) ⇒ Object
- .delegations(inherited: false) ⇒ Object
- .i18n_scope ⇒ Object
- .permit(*methods) ⇒ Object
- .permitted_params ⇒ Object
Instance Method Summary collapse
- #assign_attributes(new_attributes) ⇒ Object
- #permitted_params ⇒ Object
- #persisted? ⇒ Boolean
- #update_attributes(new_attributes) ⇒ Object
- #valid? ⇒ Boolean
Methods inherited from ViewModel::Base
Constructor Details
This class inherits a constructor from PassionView::ViewModel::Base
Class Method Details
.delegate(*methods) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/passion_view/form/base.rb', line 63 def delegate(*methods) = methods.pop unless .is_a?(Hash) && (to = [:to]) raise ArgumentError, 'Delegation needs a target' end prefix, = .values_at(:prefix, :allow_nil) cast = [:cast] writer = [:accessor] || !cast.nil? method_prefix = "#{prefix == true ? to : prefix}_" if prefix delegations << Delegation.new( to, method_prefix, methods, cast, writer, ) caster = caster_for(cast) raise ArgumentError, ':errors not allowed' if methods.include?(:errors) methods.each do |method| permit("#{method_prefix}#{method}") end if writer methods += methods.map { |method| "#{method}=" } if writer methods.each do |method| if method =~ /[^\]]=$/ define_method("#{method_prefix}#{method}") do |arg| instance_variable_set("@#{method_prefix}#{method.gsub(/=$/, '')}_before_type_cast", arg) arg = caster.call(arg) unless caster.nil? instance_eval("self.#{to}").send(method, arg) end define_method("#{method_prefix}#{method.to_s.gsub(/=$/, '')}_before_type_cast") do instance_variable_get("@#{method_prefix}#{method.gsub(/=$/, '')}_before_type_cast") || send("#{method_prefix}#{method.gsub(/=$/, '')}") end else define_method("#{method_prefix}#{method}") do |*args, &block| instance_eval("self.#{to}").try(:send, method, *args, &block) end end end end |
.delegations(inherited: false) ⇒ Object
111 112 113 114 115 |
# File 'lib/passion_view/form/base.rb', line 111 def delegations(inherited: false) @delegations ||= [] return @delegations unless inherited && superclass.respond_to?(:delegations) @delegations + superclass.delegations(inherited: true) end |
.i18n_scope ⇒ Object
5 6 7 |
# File 'lib/passion_view/form/base.rb', line 5 def self.i18n_scope :forms end |
.permit(*methods) ⇒ Object
59 60 61 |
# File 'lib/passion_view/form/base.rb', line 59 def permit(*methods) permitted_params.concat(methods) end |
.permitted_params ⇒ Object
117 118 119 |
# File 'lib/passion_view/form/base.rb', line 117 def permitted_params @permitted_params ||= [] end |
Instance Method Details
#assign_attributes(new_attributes) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/passion_view/form/base.rb', line 41 def assign_attributes(new_attributes) new_attributes.permit(*permitted_params) new_attributes.each do |k, v| send("#{k}=", v) if respond_to?("#{k}=") end end |
#permitted_params ⇒ Object
54 55 56 |
# File 'lib/passion_view/form/base.rb', line 54 def permitted_params self.class.permitted_params end |
#persisted? ⇒ Boolean
17 18 19 |
# File 'lib/passion_view/form/base.rb', line 17 def persisted? false end |
#update_attributes(new_attributes) ⇒ Object
49 50 51 52 |
# File 'lib/passion_view/form/base.rb', line 49 def update_attributes(new_attributes) assign_attributes(new_attributes) valid? && save end |
#valid? ⇒ Boolean
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/passion_view/form/base.rb', line 21 def valid? [super].concat(self.class.delegations(inherited: true).map do |delegation| item = instance_eval("self.#{delegation.to} rescue nil") next if item.nil? result = item.respond_to?(:valid?) ? item.valid? : true # TODO: tester les validations des models # du type "validates :qqch(sans _id), presence: true" if item.respond_to?(:errors) item.errors..each do |method, | .each do || errors.add(:"#{delegation.prefix}#{method}", ) unless errors[:"#{delegation.prefix}#{method}"].include?() end if item.methods.include?(method) end end result end).compact.reduce(:&) end |