Skip to content

Instantly share code, notes, and snippets.

@avit
Last active Oct 2, 2015
Embed
What would you like to do?
These are the things I think we need to define for ourselves on top of ActiveRecord

Things that exist

Model.column_names

[
  "id",
  "other_id",
  "other_type",
  "stuff",
  "things",
  "comments_count",
  "created_at",
  "updated_at"
]

Model.columns

[
  #<ActiveRecord::...::Column @name="id">,
  #<ActiveRecord::...::Column @name="other_id">,
  #<ActiveRecord::...::Column @name="other_type">,
  #<ActiveRecord::...::Column @name="stuff">,
  #<ActiveRecord::...::Column @name="things">,
  #<ActiveRecord::...::Column @name="comments_count">,
  #<ActiveRecord::...::Column @name="created_at">,
  #<ActiveRecord::...::Column @name="updated_at">
]

Model.content_columns

[
  #<ActiveRecord::...::Column @name="stuff">,
  #<ActiveRecord::...::Column @name="things">,
  #<ActiveRecord::...::Column @name="comments_count">,
  #<ActiveRecord::...::Column @name="created_at">,
  #<ActiveRecord::...::Column @name="updated_at">
]

model_instance.attributes

{
  "id" => 1,
  "other_id" => 2,
  "other_type" => Other,
  "stuff" => "A"
  "things" => "B"
  "comments_count" => 3
  "created_at" => 2015-09-23 09:52:40 -0700
  "updated_at" => 2015-09-23 09:52:40 -0700
}

Things I want

Use case: cleaning up places where I need to name the set of values slice("all", "the", "things") everywhere. When adding columns (middle_name etc.) this gets messy.

Model.data_column_names or Model::DATA_COLUMN_NAMES

[
  "stuff",
  "things"
]

This could be defined as a static whitelist on each model, or by blacklisting keys, timestamps, counter caches, etc. Essentially the result should be the assignable value columns that hold data input, not managed by ActiveRecord.

model_instance.data_attributes

{
  "stuff" => "A"
  "things" => "B"
}

Generated methods

  • data_changes
  • data_changed
  • data_changed?
  • etc.?

Naming

I can't decide on core_ or data_ as the prefix. Got any better ones?

@scottbarrow

This comment has been minimized.

Copy link

@scottbarrow scottbarrow commented Sep 23, 2015

  • 1
@markedmondson

This comment has been minimized.

Copy link

@markedmondson markedmondson commented Oct 2, 2015

  • 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment