Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom_attributes #3

Open
4 tasks
kurko opened this issue Jan 29, 2018 · 0 comments
Open
4 tasks

Support custom_attributes #3

kurko opened this issue Jan 29, 2018 · 0 comments

Comments

@kurko
Copy link
Member

kurko commented Jan 29, 2018

The contents model is quite flexible right now, but it's still a bit restrictive for things that extrapolate the title+description+images combination.

By adding a custom_attributes field in the cas_contents field, we can add any new input to the forms without having to change database schema. By configuring cas.yml, this format can be checked out on Git so developers can review changes.

Acceptance criteria

  • create cas.yml spec for defining custom attributes, which will make new input fields be shown in the forms, as well as data be automatically saved in the respective column.
  • add custom_attributes JSONB column (array) to cas_contents table.
  • create inputs in the ContentsController forms for creating and updating data, showing the fields that were defined in the cas.yml file.
  • create methods in the Cas::Content model for saving and retrieving custom attributes

Technical details

I imagine the model to have something similar to the following pseudocode:

class Cas::Content < ActiveRecord::Base
  CUSTOM_ATTRIBUTES_KEYS = ["key", "value"].freeze

  # ...

  # `custom_attributes` has the following format:
  #
  # [{
  #   "key":   "name",
  #   "value": "John Doe"
  # }]
  #
  # The type of the field (string|integer|boolean) are stored in the yml file
  def custom_attributes=(value)
    self[:custom_attributes] = value.map do |hash|
      hash.keep_if { |key, _| CUSTOM_ATTRIBUTES_KEYS.include?(key.to_s) }
    end
  end

  def custom_attribute(key)
    Array.wrap(custom_attributes).find { |attr| attr["key"] == key.to_s } || {}
  end
  
  # ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant