{"id":15463366,"url":"https://github.com/cored/shield","last_synced_at":"2026-06-18T18:32:11.222Z","repository":{"id":23064399,"uuid":"26417853","full_name":"cored/shield","owner":"cored","description":"Clean API for using policies in Rails applications","archived":false,"fork":false,"pushed_at":"2014-11-29T03:29:11.000Z","size":240,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-21T12:35:12.825Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/cored.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-10T02:36:36.000Z","updated_at":"2014-11-29T03:29:09.000Z","dependencies_parsed_at":"2022-07-31T06:38:01.097Z","dependency_job_id":null,"html_url":"https://github.com/cored/shield","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cored/shield","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cored%2Fshield","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cored%2Fshield/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cored%2Fshield/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cored%2Fshield/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cored","download_url":"https://codeload.github.com/cored/shield/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cored%2Fshield/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34503507,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-18T02:00:06.871Z","response_time":128,"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":[],"created_at":"2024-10-02T00:20:37.944Z","updated_at":"2026-06-18T18:32:11.205Z","avatar_url":"https://github.com/cored.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Shield\n\nShield is a policies clean API for using in Rails applications. The intention\nis to have an easy way to use policies inside Rails applications with a clean\nand well defined API.\n\nThe basic idea is to have some PORO to handle read operations without in case\nthose operations get a little bit complex.\n\nThe following resources brought some inspirations to the lib:\n\n[Rails - the Missing Parts - Policies](http://eng.joingrouper.com/blog/2014/03/20/rails-the-missing-parts-policies)\n\n[Doman Driven Rails - Yan Pritzker](http://www.windycityrails.org/videos/2014/#6)\n\n[7 Patterns to Refactor Fat ActiveRecord Models](http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'shield'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install shield\n\n## Usage\n\n### Simple case\n\n```ruby\nclass IsActiveUser\n  def initialize(user)\n    @user = user\n  end\n\n  def validate\n    user.email_confirmed? \u0026\u0026 user.last_login_at \u003e 14.days.ago\n  end\n\n  private\n\n  attr_reader :user\nend\n\nclass Subscriber\n  include Shield\n\n  def subscribe(user)\n    policies.with(IsActiveUser.new(user)).apply\n  end\nend\n```\n\n### Throwing exceptions\n\nWhen calling `apply!` instead of `apply` shield will generate an exception\ndynamically for you with the error message that you specified the name will be\nbased on the policy name plus the word error; ex. `IsActiveUserError`\n\n```ruby\nclass IsActiveUser\n  attr_reader :error\n\n  def initialize(user, error)\n    @error = error\n    @user = user\n  end\n\n  def validate\n    user.email_confirmed? \u0026\u0026 user.last_login_at \u003e 14.days.ago\n  end\n\n  private\n\n  attr_reader :user\nend\n\nclass Subscriber\n  include Shield\n\n  def subscribe(user)\n    policies.with(IsActiveUser.new(user, 'Policy failed')).apply!\n  end\nend\n```\n\n## Contributing\n\n1. Fork it ( https://github.com/[my-github-username]/shield/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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcored%2Fshield","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcored%2Fshield","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcored%2Fshield/lists"}