Class: Observ::PromptForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model
Defined in:
app/forms/observ/prompt_form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ PromptForm



23
24
25
26
# File 'app/forms/observ/prompt_form.rb', line 23

def initialize(attributes = {})
  super
  load_from_version if from_version.present? && name.present?
end

Instance Attribute Details

#created_byObject

For dependency injection



16
17
18
# File 'app/forms/observ/prompt_form.rb', line 16

def created_by
  @created_by
end

#persisted_promptObject (readonly)

Returns the value of attribute persisted_prompt.



17
18
19
# File 'app/forms/observ/prompt_form.rb', line 17

def persisted_prompt
  @persisted_prompt
end

Instance Method Details

#config_jsonObject

For form display - returns formatted JSON string



57
58
59
60
# File 'app/forms/observ/prompt_form.rb', line 57

def config_json
  return "" if config.blank?
  config.is_a?(String) ? config : JSON.pretty_generate(config)
end

#model_nameObject

ActiveModel compatibility for form_with



63
64
65
# File 'app/forms/observ/prompt_form.rb', line 63

def model_name
  ActiveModel::Name.new(self.class, nil, "observ_prompt_form")
end

#parsed_configObject



49
50
51
52
53
54
# File 'app/forms/observ/prompt_form.rb', line 49

def parsed_config
  return {} if config.blank?
  JSON.parse(config)
rescue JSON::ParserError
  {}
end

#persisted?Boolean



67
68
69
# File 'app/forms/observ/prompt_form.rb', line 67

def persisted?
  false
end

#saveObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/forms/observ/prompt_form.rb', line 28

def save
  return false unless valid?

  @persisted_prompt = PromptManager.create(
    name: name,
    prompt: prompt,
    config: parsed_config,
    commit_message: commit_message,
    created_by: created_by,
    promote_to_production: promote_to_production
  )

  true
rescue ActiveRecord::RecordInvalid => e
  # Copy model errors to form
  e.record.errors.each do |error|
    errors.add(error.attribute, error.message)
  end
  false
end

#to_keyObject



71
72
73
# File 'app/forms/observ/prompt_form.rb', line 71

def to_key
  nil
end

#to_modelObject



75
76
77
# File 'app/forms/observ/prompt_form.rb', line 75

def to_model
  self
end