Skip to content

Commit 7484b30

Browse files
committed
Merge pull request #6 from kaspth/infer-model
Infer model from form name.
2 parents 40e5c04 + a8d50e7 commit 7484b30

File tree

6 files changed

+17
-15
lines changed

6 files changed

+17
-15
lines changed

lib/active_form/base.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ class Base
55

66
define_model_callbacks :save, only: [:after]
77
after_save :update_form_models
8-
8+
99
delegate :persisted?, :to_model, :to_key, :to_param, :to_partial_path, to: :model
1010
attr_reader :model, :forms
11-
11+
1212
def initialize(model)
1313
@model = model
1414
@forms = []
1515
populate_forms
1616
end
17-
17+
1818
def submit(params)
1919
params.each do |key, value|
2020
if nested_params?(value)
@@ -48,12 +48,13 @@ def valid?
4848

4949
collect_errors_from(model)
5050
aggregate_form_errors
51-
51+
5252
errors.empty?
5353
end
5454

5555
class << self
56-
attr_accessor :main_class, :main_model
56+
attr_accessor :main_class
57+
attr_writer :main_model
5758
delegate :reflect_on_association, to: :main_class
5859

5960
def attributes(*names)
@@ -69,7 +70,11 @@ def attributes(*names)
6970
end
7071

7172
def main_class
72-
@main_class = main_model.to_s.camelize.constantize
73+
@main_class ||= main_model.to_s.camelize.constantize
74+
end
75+
76+
def main_model
77+
@main_model ||= name.sub(/Form$/, '').singularize
7378
end
7479

7580
alias_method :attribute, :attributes
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class ConferenceForm < ActiveForm::Base
2-
self.main_model = :conference
32
attributes :name, :city, required: true
43

54
association :speaker do
@@ -9,4 +8,4 @@ class ConferenceForm < ActiveForm::Base
98
attribute :topic, :duration, required: true
109
end
1110
end
12-
end
11+
end

test/dummy/app/forms/project_form.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class ProjectForm < ActiveForm::Base
2-
self.main_model = :project
32
attributes :name, :description, :owner_id
43

54
association :tasks do
@@ -16,7 +15,7 @@ class ProjectForm < ActiveForm::Base
1615

1716
association :project_tags do
1817
attribute :tag_id
19-
18+
2019
association :tag do
2120
attribute :name
2221
end
@@ -25,4 +24,4 @@ class ProjectForm < ActiveForm::Base
2524
association :owner do
2625
attributes :name, :description, :role
2726
end
28-
end
27+
end

test/dummy/app/forms/song_form.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class SongForm < ActiveForm::Base
2-
self.main_model = :song
32
attributes :title, :length, required: true
43

54
association :artist do
@@ -9,4 +8,4 @@ class SongForm < ActiveForm::Base
98
attributes :name, :studio, required: true
109
end
1110
end
12-
end
11+
end
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class SurveyForm < ActiveForm::Base
2-
self.main_model = :survey
32
attribute :name, required: true
43

54
association :questions do
@@ -9,4 +8,4 @@ class SurveyForm < ActiveForm::Base
98
attribute :content, required: true
109
end
1110
end
12-
end
11+
end

test/fixtures/user_form_fixture.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class UserFormFixture < ActiveForm::Base
22
self.main_model = :user
3+
34
attributes :name, :age, :gender, required: true
45

56
validates :name, length: { in: 6..20 }

0 commit comments

Comments
 (0)