{"id":13879359,"url":"https://github.com/shrinerb/shrine-mongoid","last_synced_at":"2025-08-01T19:09:15.897Z","repository":{"id":56895457,"uuid":"58323318","full_name":"shrinerb/shrine-mongoid","owner":"shrinerb","description":"Mongoid integration for Shrine","archived":false,"fork":false,"pushed_at":"2020-10-09T19:51:25.000Z","size":25,"stargazers_count":12,"open_issues_count":2,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-23T23:26:41.322Z","etag":null,"topics":["attachment","mongoid","shrine"],"latest_commit_sha":null,"homepage":"http://www.mongoid.org/","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/shrinerb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-08T17:30:56.000Z","updated_at":"2023-01-31T22:07:23.000Z","dependencies_parsed_at":"2022-08-21T01:50:19.980Z","dependency_job_id":null,"html_url":"https://github.com/shrinerb/shrine-mongoid","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/shrinerb/shrine-mongoid","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shrinerb%2Fshrine-mongoid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shrinerb%2Fshrine-mongoid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shrinerb%2Fshrine-mongoid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shrinerb%2Fshrine-mongoid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shrinerb","download_url":"https://codeload.github.com/shrinerb/shrine-mongoid/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shrinerb%2Fshrine-mongoid/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268281841,"owners_count":24225160,"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","status":"online","status_checked_at":"2025-08-01T02:00:08.611Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["attachment","mongoid","shrine"],"created_at":"2024-08-06T08:02:18.343Z","updated_at":"2025-08-01T19:09:15.840Z","avatar_url":"https://github.com/shrinerb.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Shrine::Plugins::Mongoid\n\nProvides [Mongoid] integration for [Shrine].\n\n## Installation\n\n```ruby\ngem \"shrine-mongoid\", \"~\u003e 1.0\"\n```\n\n## Usage\n\n```rb\nShrine.plugin :mongoid\n```\n```rb\nclass ImageUploader \u003c Shrine\nend\n```\n```rb\nclass Photo\n  include Mongoid::Document\n  include ImageUploader::Attachment(:image)\n\n  field :image_data, type: String # or `type: Hash`\nend\n```\n\nThe `Shrine::Attachment` module will add [model] methods, as well as\n[callbacks](#callbacks) and [validations](#validations) to tie attachment\nprocess to the record lifecycle:\n\n```rb\nphoto = Photo.new\n\nphoto.image = file # cache attachment\n\nphoto.image      #=\u003e #\u003cShrine::UploadedFile @id=\"bc2e13.jpg\" @storage_key=:cache ...\u003e\nphoto.image_data #=\u003e '{\"id\":\"bc2e13.jpg\",\"storage\":\"cache\",\"metadata\":{...}}'\n\nphoto.save # persist, promote attachment, then persist again\n\nphoto.image      #=\u003e #\u003cShrine::UploadedFile @id=\"397eca.jpg\" @storage_key=:store ...\u003e\nphoto.image_data #=\u003e '{\"id\":\"397eca.jpg\",\"storage\":\"store\",\"metadata\":{...}}'\n\nphoto.destroy # delete attachment\n\nphoto.image.exists? #=\u003e false\n```\n\n### Callbacks\n\n#### After Save\n\nAfter a record is saved, `Attacher#finalize` is called, which promotes cached\nfile to permanent storage and deletes previous file if any.\n\n```rb\nphoto = Photo.new\n\nphoto.image = file\nphoto.image.storage_key #=\u003e :cache\n\nphoto.save\nphoto.image.storage_key #=\u003e :store\n```\n\n#### After Destroy\n\nAfter a record is destroyed, `Attacher#destroy_attached` method is called,\nwhich deletes stored attached file if any.\n\n```rb\nphoto = Photo.find(photo_id)\nphoto.image #=\u003e #\u003cShrine::UploadedFile\u003e\nphoto.image.exists? #=\u003e true\n\nphoto.destroy\nphoto.image.exists? #=\u003e false\n```\n\n#### Skipping Callbacks\n\nIf you don't want the attachment module to add any callbacks to your model, you\ncan set `:callbacks` to `false`:\n\n```rb\nplugin :mongoid, callbacks: false\n```\n\n### Validations\n\nIf you're using the [`validation`][validation] plugin, the attachment module\nwill automatically merge attacher errors with model errors.\n\n```rb\nclass ImageUploader \u003c Shrine\n  plugin :validation_helpers\n\n  Attacher.validate do\n    validate_max_size 10 * 1024 * 1024\n  end\nend\n```\n```rb\nphoto = Photo.new\nphoto.image = file\nphoto.valid?\nphoto.errors #=\u003e { image: [\"size must not be greater than 10.0 MB\"] }\n```\n\n#### Attachment Presence\n\nIf you want to validate presence of the attachment, you can use ActiveModel's\npresence validator:\n\n```rb\nclass Photo\n  include Mongoid::Document\n  include ImageUploader::Attachment(:image)\n\n  validates_presence_of :image\nend\n```\n\n#### Skipping Validations\n\nIf don't want the attachment module to merge file validations errors into\nmodel errors, you can set `:validations` to `false`:\n\n```rb\nplugin :mongoid, validations: false\n```\n\n## Attacher\n\nYou can also use `Shrine::Attacher` directly (with or without the\n`Shrine::Attachment` module):\n\n```rb\nclass Photo\n  include Mongoid::Document\n\n  field :image_data, type: String # or `type: Hash`\nend\n```\n```rb\nphoto    = Photo.new\nattacher = ImageUploader::Attacher.from_model(photo, :image)\n\nattacher.assign(file) # cache\n\nattacher.file    #=\u003e #\u003cShrine::UploadedFile @id=\"bc2e13.jpg\" @storage_key=:cache ...\u003e\nphoto.image_data #=\u003e '{\"id\":\"bc2e13.jpg\",\"storage\":\"cache\",\"metadata\":{...}}'\n\nphoto.save        # persist\nattacher.finalize # promote\nphoto.save        # persist\n\nattacher.file    #=\u003e #\u003cShrine::UploadedFile @id=\"397eca.jpg\" @storage_key=:store ...\u003e\nphoto.image_data #=\u003e '{\"id\":\"397eca.jpg\",\"storage\":\"store\",\"metadata\":{...}}'\n```\n\n### Pesistence\n\nThe following persistence methods are added to `Shrine::Attacher`:\n\n| Method                    | Description                                                            |\n| :-----                    | :----------                                                            |\n| `Attacher#atomic_promote` | calls `Attacher#promote` and persists if the attachment hasn't changed |\n| `Attacher#atomic_persist` | saves changes if the attachment hasn't changed                         |\n| `Attacher#persist`        | saves any changes to the underlying record                             |\n\nSee [persistence] docs for more details.\n\n## Contributing\n\nYou can run the tests with the Rake task:\n\n```\n$ bundle exec rake test\n```\n\n## License\n\n[MIT](LICENSE.txt)\n\n[Mongoid]: https://github.com/mongodb/mongoid\n[Shrine]: https://github.com/shrinerb/shrine\n[model]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/model.md#readme\n[validation]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/validation.md#readme\n[persistence]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/persistence.md#readme\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshrinerb%2Fshrine-mongoid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshrinerb%2Fshrine-mongoid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshrinerb%2Fshrine-mongoid/lists"}