Class: Observ::PromptForm
- Inherits:
-
Object
- Object
- Observ::PromptForm
- Includes:
- ActiveModel::Attributes, ActiveModel::Model
- Defined in:
- app/forms/observ/prompt_form.rb
Instance Attribute Summary collapse
-
#created_by ⇒ Object
For dependency injection.
-
#persisted_prompt ⇒ Object
readonly
Returns the value of attribute persisted_prompt.
Instance Method Summary collapse
-
#config_json ⇒ Object
For form display - returns formatted JSON string.
-
#initialize(attributes = {}) ⇒ PromptForm
constructor
A new instance of PromptForm.
-
#model_name ⇒ Object
ActiveModel compatibility for form_with.
- #parsed_config ⇒ Object
- #persisted? ⇒ Boolean
- #save ⇒ Object
- #to_key ⇒ Object
- #to_model ⇒ Object
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_by ⇒ Object
For dependency injection
16 17 18 |
# File 'app/forms/observ/prompt_form.rb', line 16 def created_by @created_by end |
#persisted_prompt ⇒ Object (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_json ⇒ Object
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_name ⇒ Object
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_config ⇒ Object
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 |
#save ⇒ Object
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: , 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.) end false end |
#to_key ⇒ Object
71 72 73 |
# File 'app/forms/observ/prompt_form.rb', line 71 def to_key nil end |
#to_model ⇒ Object
75 76 77 |
# File 'app/forms/observ/prompt_form.rb', line 75 def to_model self end |