Class: Cms::FormEntry

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cms/form_entry.rb

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:



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_validationsCms::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.

Returns:

  • (Cms::FormEntry)

    A copy of this record with validations enabled on it.



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_paramsObject



9
10
11
# File 'app/models/cms/form_entry.rb', line 9

def permitted_params
  form.field_names
end