{"id":23654859,"url":"https://github.com/synsbasen/incrudable","last_synced_at":"2026-04-25T12:33:21.032Z","repository":{"id":268412359,"uuid":"904269221","full_name":"Synsbasen/incrudable","owner":"Synsbasen","description":"Provides a set of common CRUD actions and helpers for controllers","archived":false,"fork":false,"pushed_at":"2025-01-07T22:32:32.000Z","size":17,"stargazers_count":0,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-13T16:56:57.421Z","etag":null,"topics":["crud","dry","rails"],"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/Synsbasen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-16T15:08:00.000Z","updated_at":"2025-01-07T22:32:36.000Z","dependencies_parsed_at":"2024-12-16T16:26:34.723Z","dependency_job_id":"ddbf39ae-3f88-4edc-ade7-c23b2de07096","html_url":"https://github.com/Synsbasen/incrudable","commit_stats":null,"previous_names":["synsbasen/incrudable"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Synsbasen%2Fincrudable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Synsbasen%2Fincrudable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Synsbasen%2Fincrudable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Synsbasen%2Fincrudable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Synsbasen","download_url":"https://codeload.github.com/Synsbasen/incrudable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239621647,"owners_count":19669941,"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":["crud","dry","rails"],"created_at":"2024-12-28T19:33:44.440Z","updated_at":"2025-11-21T00:30:19.208Z","avatar_url":"https://github.com/Synsbasen.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Incrudable\n\n`Incrudable` is a Rails gem that provides a set of common CRUD actions and helpers for controllers. It is designed to standardize and simplify the implementation of controllers in your Rails applications. By including `Incrudable` in your controller, you can handle basic CRUD operations while leveraging Rails' ActiveSupport and Pundit for resource scoping and authorization.\n\n## Motivation\n\nIn Rails applications, controllers often share similar patterns for basic CRUD operations. This leads to repetitive and boilerplate code across different controllers. `Incrudable` was created to solve this problem by:\n\n- **DRY Principles**: Eliminating redundant code and ensuring controllers remain lean and consistent.\n- **Consistency**: Enforcing uniform behavior across all controllers that include `Incrudable`.\n- **Policy Enforcement**: Integrating with Pundit to globally enforce policies for authorization and scoping.\n- **Scalability**: Simplifying code maintenance as the number of controllers grows.\n- **Error Reduction**: Minimizing the chances of introducing bugs by reusing tested patterns and logic.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'incrudable'\n```\n\nAnd then execute:\n\n```bash\n$ bundle install\n```\n\nOr install it yourself as:\n\n```bash\n$ gem install incrudable\n```\n\n## Usage\n\nInclude `Incrudable` in your controller to gain access to pre-defined CRUD actions:\n\n```ruby\nclass ArticlesController \u003c ApplicationController\n  include Incrudable\nend\n```\n\n## Features\n\n### Predefined Actions\n\nThe following standard actions are available out of the box:\n\n- `index`: Fetches and sets a collection of records.\n- `show`: Fetches and sets a single record for display.\n- `new`: Prepares a new record for creation.\n- `edit`: Fetches a record for editing.\n- `create`: Handles creating a new record.\n- `update`: Handles updating an existing record.\n- `destroy`: Handles deleting a record.\n\n### Callbacks\n\n- `before_action` callbacks for setting resources:\n  - `set_record` for `show`, `edit`, `update`, and `destroy` actions.\n  - `set_records` for the `index` action.\n  - `set_new_record` for `new` and `create` actions.\n\n- `after_action` callbacks for Pundit authorization:\n  - `verify_authorized`: Ensures all actions authorize their resources.\n  - `verify_policy_scoped`: Ensures collections are policy-scoped.\n\n### Automatic Resource Identification\n\n`Incrudable` leverages the controller name to dynamically identify the resource it manages. For instance:\n\n- In a controller named `ArticlesController`, the resource is identified as `Article`.\n- The corresponding instance variables, such as `@article` or `@articles`, are automatically created and populated.\n\nThis means you don’t need to manually define methods to set these variables, reducing boilerplate and improving consistency.\n\n### Example: Controller Before and After `Incrudable`\n\n#### Before `Incrudable`\n\n```ruby\nclass ArticlesController \u003c ApplicationController\n  def index\n    @articles = policy_scope(Article)\n    authorize @articles\n  end\n\n  def show\n    @article = policy_scope(Article).find(params[:id])\n    authorize @article\n  end\n\n  def new\n    @article = Article.new\n    authorize @article\n  end\n\n  def create\n    @article = Article.new(article_params)\n    authorize @article\n    if @article.save\n      redirect_to article_path(@article), notice: \"Article created successfully.\"\n    else\n      render :new\n    end\n  end\n\n  def edit\n    @article = policy_scope(Article).find(params[:id])\n    authorize @article\n  end\n\n  def update\n    @article = policy_scope(Article).find(params[:id])\n    authorize @article\n    if @article.update(article_params)\n      redirect_to article_path(@article), notice: \"Article updated successfully.\"\n    else\n      render :edit\n    end\n  end\n\n  private\n\n  def article_params\n    params.require(:article).permit(:title, :content)\n  end\nend\n```\n\n#### After `Incrudable`\n\n```ruby\nclass ArticlesController \u003c ApplicationController\n  include Incrudable\n\n  private\n\n  def permitted_params\n    %i[title content]\n  end\n\n  def after_create_path\n    article_path(record)\n  end\n\n  def after_update_path\n    article_path(record)\n  end\nend\n```\n\nBy using `Incrudable`, you significantly reduce the code duplication and ensure consistent behavior across all controllers.\n\n### Customization Hooks\n\nOverride these methods in your controller to customize behavior:\n\n#### `permitted_params`\nDefines the attributes allowed for mass assignment. This method must be overridden in your controller.\n\n#### `after_create_path`\nSpecifies the path to redirect to after successfully creating a record. Defaults to `redirect_back fallback_location: root_path`.\n\n#### `after_update_path`\nSpecifies the path to redirect to after successfully updating a record. Defaults to `redirect_back fallback_location: root_path`.\n\n#### `after_destroy_path`\nSpecifies the path to redirect to after successfully deleting a record. Defaults to the index path of the resource.\n\n#### `record_param_identifier`\nDefines the parameter used to fetch records. Defaults to `:id`.\n\n#### `skip_authorization?`\nDefines whether to skip authorization checks for the current action. Defaults to `false`.\n\n#### `skip_policy_scope?`\nDefines whether to skip policy scope checks for the current action. Defaults to `false`.\n\n### Resource Helpers\nIf your controller does not follow standard Rails naming conventions, such as when using namespaced or custom modules, the automatic resource detection might not work as expected. In such cases, you can override the `resource` method to explicitly specify the resource class. For example:\n\n```ruby'\nclass MyModule::ArticlesController \u003c ApplicationController\n  include Incrudable\n\n  private\n\n  def resource\n    MyModule::Article\n  end\nend\n```\n\nThis ensures that `Incrudable` correctly identifies the resource and creates the corresponding instance variables like `@article` or `@articles`. This is particularly useful for applications with complex or modular architectures.\n- `record`: Fetches the current instance variable for the resource.\n\n## Localization\n\n`Incrudable` uses Rails' I18n framework for flash messages. Define translations in your locale files:\n\n```yaml\nen:\n  articles:\n    create:\n      success: \"Article created successfully.\"\n    update:\n      success: \"Article updated successfully.\"\n    destroy:\n      success: \"Article deleted successfully.\"\n```\n\n## Error Handling\n\n`Incrudable` raises `NotImplementedError` if `permitted_params` is not defined in your controller.\n\n## Dependencies\n\n- ActiveSupport::Concern\n- Pundit (for authorization and policy scoping)\n\n## License\n\nThis gem is released under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsynsbasen%2Fincrudable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsynsbasen%2Fincrudable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsynsbasen%2Fincrudable/lists"}