<commit_before># AssignsHasManyThroughRelations

Rails Engine that provides a management UI to assign models to each other in the case where they are associated in a `has_many` `:through` style relationship. 

For example, given a pair of models `Location` and `User`, we will consider `Location` to be the left side relation and `User` to be the right side relation. This UI provides a 3 column Bootstrap view where the left most column displays all `Location`s where the user can select one from the list. The 2 other columns are the Assigned and Unnassigned columns. These display `User`s that either have a join model with the selected `Location` or not, respectively. The user can then select `User`s from the Unnassigned column and the form PUT will create the join models necessary to move the Unnassigned `User`s to the middle Assigned column, effectively associating the selected `User`s from the right most column to the selected `Location`.

## What You Get
![assigns_has_many_through_relations_screenshot](https://cloud.githubusercontent.com/assets/89930/6175967/0d86cf9e-b2c9-11e4-85d8-79c58d8570d6.png)

In the above example, clicking on "Assign Selected" will move the selected `User`s to the middle column by creating the `locations_user` join model between them and the selected `Location` "Home". 

You'll notice there's a Filter text input, this will filter the corresponding list as you type. Hitting ESC will clear the text field.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'assigns_has_many_through_relations'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install assigns_has_many_through_relations

## Dependencies

1. [Twitter Bootstrap](http://getbootstrap.com) (if you want the UI to be pre-styled).
2. [jQuery](http://jquery.com/download) (for the list Filter).

## Usage

Declare the routes:

```ruby
YourApp::Application.routes.draw do
  AssignsHasManyThroughRelations.routes_for :locations, :users, self
end
```

Declare the plugin and options in your models:

```ruby
class Location < ActiveRecord::Base
  extend AssignsHasManyThroughRelations::ModelConcern
  assigns_has_many_relationships_with :location, :user
end

class User < ActiveRecord::Base
  has_many :locations_users, dependent: :delete_all
  has_many :locations, through: :locations_users
end
```

And a controller to handle the requests:

```ruby
class LocationsUsersController < ApplicationController
  extend AssignsHasManyThroughRelations::ControllerConcern
  assigns_has_many_relationships_with :location, :user
end
```

Finally, render the management UI partial in a view template in `app/views/locations_users/index.html.erb`:

```html
<div id="locations_users">
  <h1>Assign Users To Location</h1>

  <%= render '/assigns_has_many_through_relations' %>
</div>
```

You'll have to provide the user with a link to `locations_users_path`. And that's it. Now you'll be able to assign and unassign `User`s to `Location`s.

You can configure the engine to run a controller authorization method as you would a controller macro e.g like [Cancan's](https://github.com/CanCanCommunity/cancancan/wiki/Authorizing-Controller-Actions) `load_and_authorize_resource`:

```ruby
# config/initializers/assigns_has_many_through_relations.rb

AHMTR.configure do
  auth_filter :load_and_authorize_resource
end<commit_msg>tweaked README auth_filter description<commit_after># AssignsHasManyThroughRelations

Rails Engine that provides a management UI to assign models to each other in the case where they are associated in a `has_many` `:through` style relationship. 

For example, given a pair of models `Location` and `User`, we will consider `Location` to be the left side relation and `User` to be the right side relation. This UI provides a 3 column Bootstrap view where the left most column displays all `Location`s where the user can select one from the list. The 2 other columns are the Assigned and Unnassigned columns. These display `User`s that either have a join model with the selected `Location` or not, respectively. The user can then select `User`s from the Unnassigned column and the form PUT will create the join models necessary to move the Unnassigned `User`s to the middle Assigned column, effectively associating the selected `User`s from the right most column to the selected `Location`.

## What You Get
![assigns_has_many_through_relations_screenshot](https://cloud.githubusercontent.com/assets/89930/6175967/0d86cf9e-b2c9-11e4-85d8-79c58d8570d6.png)

In the above example, clicking on "Assign Selected" will move the selected `User`s to the middle column by creating the `locations_user` join model between them and the selected `Location` "Home". 

You'll notice there's a Filter text input, this will filter the corresponding list as you type. Hitting ESC will clear the text field.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'assigns_has_many_through_relations'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install assigns_has_many_through_relations

## Dependencies

1. [Twitter Bootstrap](http://getbootstrap.com) (if you want the UI to be pre-styled).
2. [jQuery](http://jquery.com/download) (for the list Filter).

## Usage

Declare the routes:

```ruby
YourApp::Application.routes.draw do
  AssignsHasManyThroughRelations.routes_for :locations, :users, self
end
```

Declare the plugin and options in your models:

```ruby
class Location < ActiveRecord::Base
  extend AssignsHasManyThroughRelations::ModelConcern
  assigns_has_many_relationships_with :location, :user
end

class User < ActiveRecord::Base
  has_many :locations_users, dependent: :delete_all
  has_many :locations, through: :locations_users
end
```

And a controller to handle the requests:

```ruby
class LocationsUsersController < ApplicationController
  extend AssignsHasManyThroughRelations::ControllerConcern
  assigns_has_many_relationships_with :location, :user
end
```

Finally, render the management UI partial in a view template in `app/views/locations_users/index.html.erb`:

```html
<div id="locations_users">
  <h1>Assign Users To Location</h1>

  <%= render '/assigns_has_many_through_relations' %>
</div>
```

You'll have to provide the user with a link to `locations_users_path`. And that's it. Now you'll be able to assign and unassign `User`s to `Location`s.

You can configure the engine to run a controller authorization method as you would a controller macro e.g like [Cancan's](https://github.com/CanCanCommunity/cancancan/wiki/Authorizing-Controller-Actions) `authorize_resource`:

```ruby
# config/initializers/assigns_has_many_through_relations.rb

AHMTR.configure do
  auth_filter :authorize_resource
end
{'fraction_non_alphanumeric': 0.07602078693392725, 'fraction_numerical': 0.011581291759465479, 'mean_word_length': 5.452107279693487, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '278076', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1888, 'n_words': 768}