All Projects → opal → Opal Rails

opal / Opal Rails

Bringing Ruby to Rails · Rails bindings for Opal

Programming Languages

ruby
36898 projects - #4 most used programming language

Labels

Projects that are alternatives of or similar to Opal Rails

Property web builder
The ultimate Ruby on Rails engine for creating real estate websites ⛺
Stars: ✭ 414 (-13.21%)
Mutual labels:  rails
Simplecov
Code coverage for Ruby with a powerful configuration library and automatic merging of coverage across test suites
Stars: ✭ 4,362 (+814.47%)
Mutual labels:  rails
Sorbet Rails
A set of tools to make the Sorbet typechecker work with Ruby on Rails seamlessly.
Stars: ✭ 464 (-2.73%)
Mutual labels:  rails
Rambulance
Simple and safe way to dynamically render error pages or JSON responses for Rails apps
Stars: ✭ 423 (-11.32%)
Mutual labels:  rails
Rubocop Rails
A RuboCop extension focused on enforcing Rails best practices and coding conventions.
Stars: ✭ 433 (-9.22%)
Mutual labels:  rails
Bootsy
Disclaimer: this project is no longer maintained.
Stars: ✭ 455 (-4.61%)
Mutual labels:  rails
Store model
Work with JSON-backed attributes as ActiveRecord-ish models
Stars: ✭ 410 (-14.05%)
Mutual labels:  rails
Attribute normalizer
Adds the ability to normalize attributes cleanly with code blocks and predefined normalizers
Stars: ✭ 473 (-0.84%)
Mutual labels:  rails
Strip attributes
🔪 An ActiveModel extension that automatically strips all attributes of leading and trailing whitespace before validation. If the attribute is blank, it strips the value to nil.
Stars: ✭ 441 (-7.55%)
Mutual labels:  rails
Hyperstack
Hyperstack ALPHA https://hyperstack.org
Stars: ✭ 463 (-2.94%)
Mutual labels:  rails
Octobox
📮Untangle your GitHub Notifications
Stars: ✭ 4,137 (+767.3%)
Mutual labels:  rails
Telegram Bot
Ruby gem for building Telegram Bot with optional Rails integration
Stars: ✭ 433 (-9.22%)
Mutual labels:  rails
Activerecord Jdbc Adapter
JRuby's ActiveRecord adapter using JDBC.
Stars: ✭ 457 (-4.19%)
Mutual labels:  rails
Rollbar Gem
Exception tracking and logging from Ruby to Rollbar
Stars: ✭ 414 (-13.21%)
Mutual labels:  rails
Godmin
Admin framework for Rails 4+
Stars: ✭ 464 (-2.73%)
Mutual labels:  rails
Hellraiser
Vulnerability scanner using Nmap for scanning and correlating found CPEs with CVEs.
Stars: ✭ 413 (-13.42%)
Mutual labels:  rails
Exception handler
💣 CUSTOM ERROR PAGES 💣 for Ruby on Rails → Translate Ruby/Rails Exceptions Into Branded 4xx/5xx HTTP Error Pages.
Stars: ✭ 455 (-4.61%)
Mutual labels:  rails
Order query
Find next / previous Active Record(s) in one query
Stars: ✭ 472 (-1.05%)
Mutual labels:  rails
Matestack Ui Core
Matestack enables you to create sophisticated, reactive UIs in pure Ruby, without touching JavaScript and HTML. You end up writing 50% less code while increasing productivity, maintainability and developer happiness.
Stars: ✭ 469 (-1.68%)
Mutual labels:  rails
Rails5 api tutorial
Learn how to build a modern API on Michael Hartl's Rails 5 tutorial
Stars: ✭ 458 (-3.98%)
Mutual labels:  rails

Opal Rails

Build Status Maintainability Gem Version fun guaranteed web scale

Rails bindings for Opal. (Changelog)

Looking for Webpack support? 👀

If you want to integrate Opal via Webpack please refer to opal-webpack-loader installation instructions.

ℹ️ Webpack and ES6 modules are not yet officially supported, but we're working on it thanks to the awesome work done in opal-webpack-loader.

Installation

In your Gemfile

gem 'opal-rails'

Add app/assets/javascript to your asset-pipeline manifest in app/assets/config/manifest.js:

bin/rails opal:install

Configuration

For the compiler

Add your configuration for the compiler:

config/initializers/opal.rb

# Compiler options
Rails.application.config.opal.method_missing_enabled   = true
Rails.application.config.opal.const_missing_enabled    = true
Rails.application.config.opal.arity_check_enabled      = true
Rails.application.config.opal.freezing_stubs_enabled   = true
Rails.application.config.opal.dynamic_require_severity = :ignore

Check out the full list of the available configuration options at: lib/opal/config.rb.

For template assigns

Add your configuration for rendering assigns when using the template handler from actions:

config/initializers/opal.rb

Rails.application.config.opal.assigns_in_templates = true
Rails.application.config.opal.assigns_in_templates = :locals # only locals
Rails.application.config.opal.assigns_in_templates = :ivars # only instance variables

Local and instance variables will be sent down to the view after converting their values to JSON.

Usage

Basic example

  1. Rename app/assets/javascripts/application.js to app/assets/javascripts/application.js.rb
  2. Replace the Sprockets directives with plain requires
# Require the opal runtime and core library
require 'opal'

# For Rails 5.1 and above, otherwise use 'opal_ujs'
require 'rails_ujs'

# Require of JS libraries will be forwarded to sprockets as is
require 'turbolinks'

# a Ruby equivalent of the require_tree Sprockets directive is available
require_tree '.'

puts "hello world!"

A more extensive example

require 'opal'
require 'opal_ujs'
require 'turbolinks'
require_tree '.' # a Ruby equivalent of the require_tree Sprockets directive is available

# ---- YOUR FANCY RUBY CODE HERE ----
#
# Examples:

# == Print something in the browser's console
puts "Hello world!"
pp hello: :world
require 'console'
$console.log %w[Hello world!]

# == Use Native to wrap native JS objects, $$ is preconfigured to wrap `window`
require 'native'
$$.alert "Hello world!"

# == Do some DOM manipulation with jQuery
require 'opal-jquery'
Document.ready? do
  Element.find('body').html = '<h1>Hello world!</h1>'
end

# == Or access the DOM api directly
$$[:document].addEventListener(:DOMContentLoaded, -> {
  $$[:document].querySelector('body')[:innerHTML] = '<h1>Hello world!</h1>'
})

Using Sprockets directives and application.js

If you want to use application.js (instead of application.js.rb) and keep using Sprockets directives, you'll need to load the Opal files you require via Sprockets manually, e.g.:

//= require opal
//= require rails_ujs
//= require turbolinks
//= require_tree .
//= require app

Opal.require('opal');
Opal.require('app');

As a template

You can use it for your views too:

# app/controllers/posts_controller.rb

def create
  @post = Post.create!(params[:post])
  render type: :js, locals: {comments_html: render_to_string(@post.comments)}
end

Assigned instance that would normally be available in your views are converted to JSON objects first.

# app/views/posts/create.js.opal

post = Element.find('.post')
post.find('.title').html    = @post[:title]
post.find('.body').html     = @post[:body]
post.find('.comments').html = comments_html

Instance and local variables in templates

By default opal-rails will forward any instance and local variable you'll pass to the template.

This behavior can be disabled by setting Rails.application.config.opal.assigns_in_templates to false in config/initializers/assets.rb:

Rails.application.config.opal.assigns_in_templates = false

As a Haml filter (optional)

Of course you need to require haml-rails separately since its presence is not assumed

-# app/views/posts/show.html.haml

%article.post
  %h1.title= post.title
  .body= post.body

%a#show-comments Display Comments!

.comments(style="display:none;")
  - post.comments.each do |comment|
    .comment= comment.body

:opal
  Document.ready? do
    Element.find('#show-comments').on :click do |click|
      click.prevent_default
      click.current_target.hide
      Element.find('.comments').effect(:fade_in)
    end
  end

RSpec support

Extracted to (unreleased) opal-rspec-rails

Add this line to your Gemfile:

gem 'opal-rspec-rails', github: 'opal/opal-rspec-rails'

Minitest support

Upcoming as opal-minitest-rails

Shared templates

As long as the templates are inside the Sprockets/Opal load path, then you should be able to just require them.

Let's say we have this template app/views/shared/test.haml:

.row
  .col-sm-12
    = @bar

We need to make sure Opal can see and compile that template. So we need to add the path to sprockets:

# config/initializers/opal.rb
Rails.application.config.assets.paths << Rails.root.join('app', 'views', 'shared').to_s

Now, somewhere in application.rb you need to require that template, and you can just run it through Template:

# app/assets/javascripts/application.rb
require 'opal'
require 'opal-haml'
require 'test'

@bar = "hello world"

template = Template['test']
template.render(self)
# =>  '<div class="row"><div class="col-sm-12">hello world</div></div>'

Using Ruby gems from Opal

Just use Opal.use_gem in your asset initializer (config/initializers/assets.rb).

Example:

Opal.use_gem 'cannonbol'

Contributing

Run the specs:

bin/setup
bin/rake

Inspect the test app:

bin/rackup
# visit localhost:9292

Tinker with a sandbox app:

bin/sandbox # will re-create the app
bin/rails s # will start the sandbox app server
# visit localhost:3000

License

© 2012-2021 Elia Schito

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].