Class: Cms::FormEntry
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Cms::FormEntry
- Defined in:
- app/models/cms/form_entry.rb
Class Method Summary collapse
-
.dup(&block) ⇒ Object
Creates a faux class with singleton methods.
-
.for(form) ⇒ Object
Create an Entry for a specific Form.
-
.ish(*args, &block) ⇒ Object
Create an instance of a FormEntry with the given methods.
- .search(term) ⇒ Object
Instance Method Summary collapse
-
#enable_validations ⇒ Cms::FormEntry
Returns a copy of the persisted object.
- #permitted_params ⇒ Object
Class Method Details
.dup(&block) ⇒ Object
Creates a faux class with singleton methods. Solves this problem:
https://github.com/rails/rails/issues/5449
51 52 53 54 55 56 57 58 59 |
# File 'app/models/cms/form_entry.rb', line 51 def dup(&block) super.tap do |dup| def dup.name() FormEntry.name end dup.class_eval(&block) if block end end |
.for(form) ⇒ Object
Create an Entry for a specific Form. It will have validation and accessors based on the fields of the form.
33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/cms/form_entry.rb', line 33 def for(form) entry = FormEntry.ish(form: form) { form.field_names.each do |field_name| if form.required?(field_name) validates field_name, presence: true end end } entry end |
.ish(*args, &block) ⇒ Object
Create an instance of a FormEntry with the given methods.
45 46 47 |
# File 'app/models/cms/form_entry.rb', line 45 def ish(*args, &block) dup(&block).new(*args) end |
.search(term) ⇒ Object
26 27 28 |
# File 'app/models/cms/form_entry.rb', line 26 def search(term) where("data_columns like ?", "%#{term}%") end |
Instance Method Details
#enable_validations ⇒ Cms::FormEntry
Returns a copy of the persisted object. This is required so that existing records fetched from the db can have validation during update operations.
17 18 19 20 21 22 |
# File 'app/models/cms/form_entry.rb', line 17 def enable_validations entry = FormEntry.for(form) entry.attributes = self.attributes entry.instance_variable_set(:@new_record, false) entry end |
#permitted_params ⇒ Object
9 10 11 |
# File 'app/models/cms/form_entry.rb', line 9 def permitted_params form.field_names end |