{"id":26378092,"url":"https://github.com/panorama-ed/rails_external_fields","last_synced_at":"2025-03-17T04:21:07.224Z","repository":{"id":29703845,"uuid":"33246646","full_name":"panorama-ed/rails_external_fields","owner":"panorama-ed","description":"Create the illusion that an object has specific attributes when those attributes actually belong to an associated object.","archived":false,"fork":false,"pushed_at":"2023-11-29T20:33:58.000Z","size":41,"stargazers_count":6,"open_issues_count":3,"forks_count":4,"subscribers_count":42,"default_branch":"main","last_synced_at":"2024-04-25T21:21:24.761Z","etag":null,"topics":["open-source-project","rails","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/panorama-ed.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-04-01T12:38:35.000Z","updated_at":"2024-08-05T18:22:53.060Z","dependencies_parsed_at":"2024-08-05T18:35:32.858Z","dependency_job_id":null,"html_url":"https://github.com/panorama-ed/rails_external_fields","commit_stats":{"total_commits":34,"total_committers":8,"mean_commits":4.25,"dds":0.4117647058823529,"last_synced_commit":"63ccbc1af0e7007a49891f1f54c485a7c3a373fe"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/panorama-ed%2Frails_external_fields","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/panorama-ed%2Frails_external_fields/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/panorama-ed%2Frails_external_fields/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/panorama-ed%2Frails_external_fields/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/panorama-ed","download_url":"https://codeload.github.com/panorama-ed/rails_external_fields/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243878436,"owners_count":20362432,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["open-source-project","rails","ruby"],"created_at":"2025-03-17T04:21:06.608Z","updated_at":"2025-03-17T04:21:07.213Z","avatar_url":"https://github.com/panorama-ed.png","language":"Ruby","readme":"[![Code Coverage](https://codecov.io/gh/panorama-ed/rails_external_fields/branch/main/graph/badge.svg)](https://codecov.io/gh/panorama-ed/rails_external_fields)\n[![Build Status](https://travis-ci.com/panorama-ed/rails_external_fields.svg)](https://travis-ci.com/panorama-ed/rails_external_fields)\n[![Inline docs](http://inch-ci.org/github/panorama-ed/rails_external_fields.png)](http://inch-ci.org/github/panorama-ed/rails_external_fields)\n[![Gem Version](https://badge.fury.io/rb/external_fields.svg)](http://badge.fury.io/rb/external_fields)\n\n# ExternalFields\nCreate the illusion that an object has specific attributes when those attributes\nactually belong to an associated object.\n\nThis is particularly useful for different classes within a single-\ntable inheritance table to have access to separate fields in class-specific\nassociations.\n\n## Installation\nAdd this line to your application's Gemfile:\n\n```\ngem \"external_fields\"\n```\n\nAnd then execute:\n\n```\n$ bundle\n```\n\nOr install it yourself as:\n\n```\n$ gem install external_fields\n```\n\n## Usage\nInclude `ExternalFields` and define the external fields using the `external_field` method. For example, if `grade_level`, `age` and `credits` are defined in another class `StudentData` and you want to access them in `Student` you could do:\n\n```ruby\nrequire \"active_record\"\nrequire \"active_support\"\n\nrequire \"external_fields\"\n\nclass Student \u003c ActiveRecord::Base\n  include ExternalFields\n\n  has_one :data,\n          class_name: StudentData\n\n  external_field :grade_level,              # External attribute 1\n                 :age,                      # External attribute 2\n                 :credits,                  # External attribute 3\n                 :data,                     # Name of the association\n                 class_name: \"StudentData\", # Class name of association\n                 save_empty: false          # Don't save empty associations\nend\n```\n\nwhere the external fields are defined in another associated class:\n\n```ruby\nclass StudentData \u003c ActiveRecord::Base\n  attr_accessor :grade_level, :age, :credits\nend\n```\n\nNow you can directly call the accessors on the `Student` objects:\n\n```ruby\n \u003e s = Student.create!\n \u003e s.age\n=\u003e nil\n\n \u003e s.age = 10\n \u003e s.age\n=\u003e 10\n\n \u003e s.grade_level = 4\n \u003e s.grade_level\n=\u003e 4\n```\n\n### Overriding default behavior using `underscored` accessors\nYou can also add underscored accessors using the `underscore` flag\n\n```ruby\n...\n  external_field :grade_level,             # External attribute 1\n                 :age,                     # External attribute 2\n                 :credits,                 # External attribute 3\n                 :data,                    # Name of the association\n                 class_name: \"StudentData\" # Class name of association\n                 underscore: true          # Flag for underscored accessors\n...\n```\n\nThis will allow you to use the external fields using underscored methods:\n```ruby\ns = Student.create!\ns._age\ns._grade_level\n```\n\nThis approach lets you override the default behavior cleanly. For example,\nyou could override the grade level using this method:\n\n```ruby\ndef grade_level\n  if _grade_level == 0\n    \"Kindergarten\"\n  else\n    _grade_level\n  end\nend\n```\n\n### Overriding default behavior using `save_empty: false`\n**This is the recommended configuration to use for all new code.**\n\nTo avoid unnecessary writes, you can rely on empty-valued class instances so\nthat external associations are only saved when they have one or more attributes\nwith non-default values.\n\nFor any given association class, its constructor defines the attribute values\nfor an \"empty\" instance. This means that, in the below example, retreival of\n`data` will return `StudentData.new` if there's no `StudentData` record saved.\nIf `set_empty: true` were configured instead, calling `data` would still return\n`StudentData.new`, but it would also write the empty record to the database.\n\n```ruby\n  external_field :grade_level,              # External attribute 1\n                 :age,                      # External attribute 2\n                 :credits,                  # External attribute 3\n                 :data,                     # Name of the association\n                 class_name: \"StudentData\", # Class name of association\n                 save_empty: false          # Don't save empty associations\n```\n\nThe default value for `save_empty` is `true` only for backward compatability, \nas existing code using this gem may rely on empty rows existing in a database.\n\n### Accessing the original association\n\nIn some instances it's helpful to be able to use the original association\nwithout building an object on access. For instance, you might want to have a\nvalidation inspect a value without creating a new object on each save. In that\ncase, you can use the `use_original` flag on the association like so:\n\n```ruby\nvalidate :kindergarten_students_have_names\n\ndef kindergarten_students_have_names\n  data_obj = data(use_original: true)\n\n  if data_obj \u0026\u0026 grade_level == \"Kindergarten\" \u0026\u0026 name.blank?\n    # Note that `name` is an attribute on `Student` but `grade_level`\n    # is accessed through the `data` association as defined earlier\n    # in the README.\n    errors.add(:name, \"must be present for kindergarten students\")\n  end\nend\n```\n\n## Documentation\n\nWe have documentation on [RubyDoc](http://www.rubydoc.info/github/panorama-ed/rails_external_fields/main).\n\n## Contributing\n\n1. Fork it (https://github.com/panorama-ed/rails_external_fields/fork)\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\n**Make sure your changes have appropriate tests (`bundle exec rspec`)\nand conform to the Rubocop style specified.** We use\n[overcommit](https://github.com/causes/overcommit) to enforce good code.\n\n## License\n\n`ExternalFields` is released under the\n[MIT License](https://github.com/panorama-ed/rails_external_fields/blob/main/LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpanorama-ed%2Frails_external_fields","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpanorama-ed%2Frails_external_fields","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpanorama-ed%2Frails_external_fields/lists"}