{"id":14955274,"url":"https://github.com/ryancyq/mime_actor","last_synced_at":"2025-08-21T15:08:31.429Z","repository":{"id":245390499,"uuid":"818193023","full_name":"ryancyq/mime_actor","owner":"ryancyq","description":"Action processing with Callback + Rescue handlers for different MIME types in Rails.","archived":false,"fork":false,"pushed_at":"2025-03-21T14:04:05.000Z","size":676,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-04T05:17:30.308Z","etag":null,"topics":["actioncontroller","actionpack","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/ryancyq.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"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":"2024-06-21T09:49:55.000Z","updated_at":"2025-03-21T14:04:08.000Z","dependencies_parsed_at":"2024-08-07T22:06:44.539Z","dependency_job_id":"5a60b31d-f47e-4b79-b635-6df89383e592","html_url":"https://github.com/ryancyq/mime_actor","commit_stats":{"total_commits":430,"total_committers":3,"mean_commits":"143.33333333333334","dds":0.05581395348837215,"last_synced_commit":"3baa97dd3dc9d10c7829d60e50356729dfa9bafb"},"previous_names":["ryancyq/mime_actor"],"tags_count":28,"template":false,"template_full_name":null,"purl":"pkg:github/ryancyq/mime_actor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryancyq%2Fmime_actor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryancyq%2Fmime_actor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryancyq%2Fmime_actor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryancyq%2Fmime_actor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryancyq","download_url":"https://codeload.github.com/ryancyq/mime_actor/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryancyq%2Fmime_actor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271499904,"owners_count":24770369,"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-21T02:00:08.990Z","response_time":74,"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":["actioncontroller","actionpack","rails","ruby"],"created_at":"2024-09-24T13:10:48.015Z","updated_at":"2025-08-21T15:08:31.358Z","avatar_url":"https://github.com/ryancyq.png","language":"Ruby","readme":"# mime_actor\n\n[![Version][rubygems_badge]][rubygems]\n[![CI][ci_badge]][ci_workflows]\n[![Coverage][coverage_badge]][coverage]\n[![Maintainability][maintainability_badge]][maintainability]\n\nAction processing with Callback + Rescue handlers for different MIME types in Rails.\n\nIn most of the Rails apps, a single `ActionController` is only responsible for rendering a single MIME type. That is usually done by calling `ActionController#respond_to` for each action in the controller.\n\n```rb\nclass EventsController \u003c ActionController::Base\n  def index\n    @events = Event.all\n    respond_to :json\n  end\n\n  def show\n    respond_to do |format|\n      format.json { render json: @event }\n    end\n  end\nend\n```\n\nHowever, it can be a litte bit messy when some actions render more than a single MIME type with error handling for different MIME type. See [comparison][doc_comparison] to understand more.\n\n## Usage\n\n**Mime Actor** allows you to do something like:\n\n#### Customize Action for certain MIME type\n```rb\n  act_on_action :index, :show, format: %i[html json]\n  act_on_action :create, format: :json\n\n  def index\n    @events = Event.all\n  end\n  def show\n    @event = Event.find(params[:id])\n  end\n  def create\n    @event = Event.new.save!\n  end\n```\nThe above is equivalent to the following:\n```rb\n  def index\n    respond_to do |format|\n      format.html { @events = Event.all }\n      format.json { @events = Event.all }\n    end\n  end\n  def show\n    respond_to do |format|\n      format.html { @event = Event.find(params[:id]) }\n      format.json { @event = Event.find(params[:id]) }\n    end\n  end\n  def create\n    respond_to do |format|\n      format.json { @event = Event.new.save! }\n    end\n  end\n```\n\n#### Callback registration for certain Action + MIME type\n```rb\n  act_before :load_event_categories, action: :index, format: :html\n\n  act_on_action :index, :show, format: %i[html json]\n\n  def index\n    @events = Event.all\n  end\n\n  def show\n    @event = Event.find(params[:id])\n  end\n\n  def load_event_categories\n    @event_categories = EventCategory.all\n  end\n```\nThe above is equivalent to the following:\n```rb\n  def index\n    respond_to do |format|\n      format.html do \n        load_event_categories\n        @events = Event.all\n      end\n      format.json { @events = Event.all }\n    end\n  end\n\n  def show\n    respond_to do |format|\n      format.html { @event = Event.find(params[:id]) }\n      format.json { @event = Event.find(params[:id]) }\n    end\n  end\n\n  def load_event_categories\n    @event_categories = EventCategory.all\n  end\n```\n\n#### Rescue handler registration for certain Action + MIME type\n```rb\n  act_before :load_event, action: %i[show update]\n\n  act_on_action :show, :update, format: :json\n  act_on_action :show, format: :html\n\n  rescue_act_from ActiveRecord::RecordNotFound, ActiveRecord::RecordNotSaved, format: :json, with: :handle_json_error\n  rescue_act_from ActiveRecord::RecordNotSaved, action: :show, format: :html, with: -\u003e { redirect_to \"/events\" }\n  rescue_act_from ActiveRecord::RecordNotSaved, action: :update, format: :html, with: -\u003e { render :edit }\n\n  def show\n    # ...\n  end\n\n  def update\n    @event.name = params[:name]\n    @event.save!\n    redirect_to \"/events/#{@event.id}\"\n  end\n\n  def load_event\n    @event = Event.find(params[:id])\n  end\n\n  def handle_json_error(error)\n    render status: :bad_request, json: { error: error.message }\n  end\n```\nThe above is equivalent to the following:\n```rb\n  before_action :load_event, only: %i[show update]\n\n  rescue_from ActiveRecord::RecordNotFound, with: :handle_not_found_error\n  rescue_from ActiveRecord::RecordNotSaved, with: :handle_not_saved_error\n\n  def show\n    respond_to do |format|\n      format.html { ... }\n      format.json { ... }\n    end\n  end\n\n  def update\n    @event.name = params[:name]\n    @event.save!\n    redirect_to \"/events/#{@event.id}\"\n  end\n\n  def handle_not_found_error(error)\n    respond_to do |format|\n      format.html { redirect_to \"/events\" }\n      format.json { handle_json_error(error) }\n    end\n  end\n\n  def handle_not_saved_error(error)\n    respond_to do |format|\n      format.html { render :edit }\n      format.json { handle_json_error(error) }\n    end\n  end\n\n  def handle_json_error(error)\n    render status: :bad_request, json: { error: error.message }\n  end\n```\n\n## Features\n\n- Action customisation for [ActionController][doc_action_controller] per MIME type\n- Customisation are deduplicated automatically when configured multiple times\n- Flexible rescue handler definition for the combination of Action/MIME type or both\n- Built on top of [ActionController::Metal::MimeResponds][doc_action_controller_mime_responds]\n- Fully compatible with other [ActionController][doc_action_controller] functionalities \n\n## Documentation\n\nYou can find the documentation on [RubyDoc][doc_mime_actor].\n\n## Requirements\n\n- Ruby: MRI 2.5+\n- ActiveSupport: 6.1+\n- ActionPack: 6.1+\n\n## Installation\n\nInstall the gem and add to the application's Gemfile by executing:\n```sh\nbundle add mime_actor\n```\n\nIf bundler is not being used to manage dependencies, install the gem by executing:\n```sh\ngem install mime_actor\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## License\nPlease see [LICENSE](https://github.com/ryancyq/mime_actor/blob/main/LICENSE) for licensing details.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at [https://github.com/ryancyq/mime_actor](https://github.com/ryancyq/mime_actor).\n\n[rubygems_badge]: https://img.shields.io/gem/v/mime_actor.svg\n[rubygems]: https://rubygems.org/gems/mime_actor\n[ci_badge]: https://github.com/ryancyq/mime_actor/actions/workflows/ci.yml/badge.svg\n[ci_workflows]: https://github.com/ryancyq/mime_actor/actions/workflows/ci.yml\n[coverage_badge]: https://codecov.io/gh/ryancyq/mime_actor/graph/badge.svg?token=4C091RHXC3\n[coverage]: https://codecov.io/gh/ryancyq/mime_actor\n[maintainability_badge]: https://api.codeclimate.com/v1/badges/06689606dc3f3945dc1b/maintainability\n[maintainability]: https://codeclimate.com/github/ryancyq/mime_actor/maintainability\n\n[doc_mime_actor]: https://rubydoc.info/gems/mime_actor\n[doc_action_controller]: https://rubydoc.info/gems/actionpack/ActionController/Metal\n[doc_action_controller_mime_responds]: https://rubydoc.info/gems/actionpack/ActionController/MimeResponds\n[doc_comparison]: https://github.com/ryancyq/mime_actor/blob/main/COMPARE.md\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryancyq%2Fmime_actor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryancyq%2Fmime_actor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryancyq%2Fmime_actor/lists"}