|
1 | | -require 'active_form/abstract_form' |
| 1 | +require 'active_form/model_association' |
| 2 | +require 'active_form/collection_association' |
| 3 | +require 'active_form/associatable' |
2 | 4 |
|
3 | 5 | module ActiveForm |
4 | | - class Base < AbstractForm |
5 | | - include ActiveModel::Model |
6 | | - |
7 | | - delegate :persisted?, :to_model, :to_key, :to_param, :to_partial_path, to: :model |
8 | | - attr_reader :model |
9 | | - |
10 | | - def initialize(model) |
11 | | - @model = model |
12 | | - |
13 | | - @forms = [] |
14 | | - populate_forms |
| 6 | + class Base |
| 7 | + extend Associatable |
| 8 | + |
| 9 | + # SignupForm.new(user: User.find(params[:user_id])) |
| 10 | + def initialize(models = nil) |
| 11 | + if models.respond_to?(:each) |
| 12 | + assign_attributes(models) |
| 13 | + else |
| 14 | + main_association.instance = models |
| 15 | + end |
15 | 16 | end |
16 | 17 |
|
17 | 18 | def save |
18 | 19 | return false unless valid? |
19 | 20 |
|
20 | | - if ActiveRecord::Base.transaction { model.save } |
21 | | - forms.each(&:reset) |
| 21 | + ActiveRecord::Base.transaction do |
| 22 | + main_association.save |
22 | 23 | end |
23 | 24 | end |
24 | 25 |
|
25 | | - class << self |
26 | | - attr_accessor :main_model |
27 | | - delegate :reflect_on_association, to: :model_class |
28 | | - |
29 | | - def attributes(*names) |
30 | | - options = names.pop if names.last.is_a?(Hash) |
31 | | - |
32 | | - if options && options[:required] |
33 | | - validates_presence_of *names |
34 | | - end |
35 | | - |
36 | | - names.each do |attribute| |
37 | | - delegate attribute, "#{attribute}=", to: :model |
38 | | - end |
39 | | - end |
| 26 | + def submit(params) |
| 27 | + assign_attributes(params) |
| 28 | + end |
40 | 29 |
|
41 | | - alias_method :attribute, :attributes |
| 30 | + mattr_accessor :main_model, instance_writer: false |
42 | 31 |
|
43 | | - def association(name, options = {}, &block) |
44 | | - define_method(name) { instance_variable_get("@#{name}").models } |
45 | | - define_method("#{name}_attributes=") {} |
| 32 | + delegate :id, :persisted?, :to_model, :to_partial_path, to: :main_association |
46 | 33 |
|
47 | | - forms << [name, options, block] |
| 34 | + class << self |
| 35 | + delegate :attributes, :association_scope, to: :main_association |
| 36 | + |
| 37 | + def main_association |
| 38 | + @@main_association ||= \ |
| 39 | + if main_model |
| 40 | + ModelAssociation.new(main_model) |
| 41 | + else |
| 42 | + raise ArgumentError, "you need to set the main_model for this form," \ |
| 43 | + " like self.main_model = :article" |
| 44 | + end |
48 | 45 | end |
| 46 | + end |
49 | 47 |
|
50 | | - def forms |
51 | | - @forms ||= [] |
| 48 | + private |
| 49 | + def respond_to_missing?(meth, include_private = false) |
| 50 | + main_association.respond_to?(meth) |
52 | 51 | end |
53 | 52 |
|
54 | | - private |
55 | | - def model_class |
56 | | - @model_class ||= main_model.to_s.camelize.constantize |
| 53 | + def method_missing(meth, *args, &block) |
| 54 | + if main_association.respond_to?(meth) |
| 55 | + main_association.send(meth, *args, &block) |
| 56 | + else |
| 57 | + super |
57 | 58 | end |
58 | | - end |
| 59 | + end |
59 | 60 |
|
60 | | - private |
| 61 | + def main_association |
| 62 | + @@main_association |
| 63 | + end |
61 | 64 |
|
62 | | - def populate_forms |
63 | | - self.class.forms.each do |(name, options, block)| |
64 | | - FormDefinition.new(name, block, options).build_for(model).tap do |form| |
65 | | - forms << form |
66 | | - instance_variable_set("@#{name}", form) |
67 | | - end |
| 65 | + def assign_attributes(attributes) |
| 66 | + attributes.each do |key, value| |
| 67 | + self.public_send("#{key}=", value) |
| 68 | + end if attributes |
68 | 69 | end |
69 | | - end |
70 | 70 | end |
71 | 71 | end |
0 commit comments