diff --git a/app/models/answer.rb b/app/models/answer.rb index c9e3d6c..bec8f3c 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -63,7 +63,7 @@ answer_copy.save! return answer_copy end - + # This method helps to decide if an answer option (:radiobuttons, :checkbox, etc ) in form views should be checked or not # Returns true if the given option_id is present in question_options, otherwise returns false def has_question_option(option_id) @@ -86,4 +86,37 @@ def non_archived_notes return notes.select{ |n| n.archived.blank? }.sort!{ |x,y| y.updated_at <=> x.updated_at } end + + + ## + # Returns the parsed JSON hash for the current answer object + # Generates a new hash if none exists for rda_questions + # + # @return [Hash] the parsed hash of the answer. + # Should have keys 'standards', 'text' + # 'standards' is a list of : pairs + # 'text' is the text from the comments box + def answer_hash + default = {'standards' => {}, text => ''} + begin + h = self.text.nil? ? default : JSON.parse(answer.text) + rescue JSON::ParserError => e + h = default + end + return h + end + + ## + # Given a hash of standards and a comment value, this updates answer + # text for rda_questions + # + # @param [standards] a hash of standards + # @param [text] option comment text + # nothing returned, but the status of the text field of the answer is changed + def update_answer_hash(standards={},text="") + h = {} + h[:standards] = standards + h[:text] = text + self.text = h.to_json + end end diff --git a/app/models/question_format.rb b/app/models/question_format.rb index b7d6afc..b6d1e36 100644 --- a/app/models/question_format.rb +++ b/app/models/question_format.rb @@ -4,11 +4,11 @@ # Associations has_many :questions - enum formattype: [ :textarea, :textfield, :radiobuttons, :checkbox, :dropdown, :multiselectbox, :date ] + enum formattype: [ :textarea, :textfield, :radiobuttons, :checkbox, :dropdown, :multiselectbox, :date, :rda_metadata ] attr_accessible :formattype - + validates :title, presence: {message: _("can't be blank")}, uniqueness: {message: _("must be unique")} - + ## # Possibly needed for active_admin # -relies on protected_attributes gem as syntax depricated in rails 4.2 @@ -16,7 +16,7 @@ # Retrieves the id for a given formattype passed scope :id_for, -> (formattype) { where(formattype: formattype).pluck(:id).first } - + ## # Define Bit Field Values so we can test a format without doing string comps # Column type diff --git a/config/routes.rb b/config/routes.rb index ee376eb..4de6205 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -172,6 +172,9 @@ post 'create_or_update', on: :collection end + # Question Formats controller, currently just the one action + get 'question_formats/rda_api_address' => 'question_formats#rda_api_address' + resources :notes, only: [:create, :update, :archive] do member do patch 'archive' @@ -302,7 +305,7 @@ get 'unpublish', action: :unpublish, constraints: {format: [:json]} end end - + get 'template_options' => 'templates#template_options', constraints: {format: [:json]} get 'download_plans' => 'plans#download_plans' end