Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/itmammoth/with_popup

A rubygem for rails application to open and manage a popup window
https://github.com/itmammoth/with_popup

gem javascript rails ruby

Last synced: 8 days ago
JSON representation

A rubygem for rails application to open and manage a popup window

Awesome Lists containing this project

README

        

WithPopup
====

WithPopup makes it easy to open and manage a popup window in your rails application, and which is NOT controlled by popup blocker.

## Demo

![Demo](https://raw.githubusercontent.com/itmammoth/with_popup/master/demo.gif "Demo")

## Requirement

* rails ~> 4.0
* jquery-rails >= 3.0, < 5.0

## Installation

Add this in your ```Gemfile```, and run the ```bundle install``` command.

```ruby
gem 'with_popup'
```

Add this in your ```app/assets/javascripts/application.js``` below jquery.

```Javascript
//= require jquery
//= require jquery_ujs
//= require with_popup
//= require_tree .
```

## Usage

WithPopup provides some helper methods managing a popup window in view and controller contexts, which are wrapping form helper methods that create clickable DOM (like ```link_to```, ```submit_tag```) with ```*_with_popup``` naming.

Typical usage is like this.

```HTML+ERB
# In your erb file
<%= form_for @post do |f| %>
...
# Submit while opening a popup window
<%= f.submit_with_popup %>
<% end %>
```

```Ruby
# In your controller
def create
@post = Post.new(params[:post].permit(...))
if @post.save
# Show print preview in the popup window you've opened
reload_popup print_post_path(@post)
redirect_to @post
else
# Close the popup window
close_popup
render :new
end
end
```

#### Note:
These ```*_with_popup``` methods never interrupt the original click actions, just open popup windows simultaneously.

### Form helper methods

All helper methods are defined to deledate the processing to the original methods they are wrapping, so you can invoke them as you do with the original methods.

#### FormTagHelper (ActionView::Base)

| Methods | Wrapped method |
|-----------------------------------|------------------------|
| submit_with_popup_tag | submit_tag |
| image_submit_with_popup_tag | image_submit_tag |
| button_with_popup_tag | button_tag |
| button_with_popup_to | button_to |
| link_with_popup_to | link_to |
| link_with_popup_to_if | link_to_if |
| link_with_popup_to_unless | link_to_unless |
| link_with_popup_to_unless_current | link_to_unless_current |

#### FormHelper (ActionView::Helpers::FormBuilder)

| Methods | Wrapped method |
|-------------------|----------------|
| submit_with_popup | submit |
| button_with_popup | button |

### Option

You can give a particular option for the javascript ```window.open``` function.

```HTML+ERB
# In your erb file
<%= submit_with_popup_tag 'Create and Print',
with_popup: { src: '/loading', features: 'width=300,height=200' } %>
```

Its popup window will be opened like this

```Javascript
window.open('', '/loading', 'width=300,height=200');
```

### Controller methods

| Methods | Description |
|--------------------|---------------------------------------------------------|
| realod_popup(path) | Reload the popup window you opened with indicated path. |
| close_popup | Close the popup window you opened. |

## Test helpers
WithPopup provides some test helper methods.
```ruby
# In your rails_helper.rb

require 'with_popup/test_helpers'

...

RSpec.configure do |config|
...
config.include WithPopup::TestHelpers::Controller, type: :controller
...
end
```

Then, you can invoke the methods below in controller test context.
```ruby
it { expect(with_popup_reloading_path).to match '/reload/path' }
it { expect(with_popup_is_closing?).to be_truthy }
```

## Contribution
* Fork the master branch, and clone it.
* Run the ```bundle install``` command to start developing.
* Test it with the ```bundle exec rspec``` command.
* Shoot a pull-request, thank you.

## Licence

MIT

## Author

[itmammoth](https://github.com/itmammoth)