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
- .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
i18n_scope, #initialize, lookup_ancestors
Constructor Details
This class inherits a constructor from PassionView::ViewModel::Base
Class Method Details
.delegate(*methods) ⇒ Object
59 60 61 62 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 |
# File 'lib/passion_view/form/base.rb', line 59 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
107 108 109 110 111 |
# File 'lib/passion_view/form/base.rb', line 107 def delegations(inherited: false) @delegations ||= [] return @delegations unless inherited && superclass.respond_to?(:delegations) @delegations + superclass.delegations(inherited: true) end |
.permit(*methods) ⇒ Object
55 56 57 |
# File 'lib/passion_view/form/base.rb', line 55 def permit(*methods) permitted_params.concat(methods) end |
.permitted_params ⇒ Object
113 114 115 |
# File 'lib/passion_view/form/base.rb', line 113 def permitted_params @permitted_params ||= [] end |
Instance Method Details
#assign_attributes(new_attributes) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/passion_view/form/base.rb', line 37 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
50 51 52 |
# File 'lib/passion_view/form/base.rb', line 50 def permitted_params self.class.permitted_params end |
#persisted? ⇒ Boolean
13 14 15 |
# File 'lib/passion_view/form/base.rb', line 13 def persisted? false end |
#update_attributes(new_attributes) ⇒ Object
45 46 47 48 |
# File 'lib/passion_view/form/base.rb', line 45 def update_attributes(new_attributes) assign_attributes(new_attributes) valid? && save end |
#valid? ⇒ Boolean
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/passion_view/form/base.rb', line 17 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 |