{"id":13463264,"url":"https://github.com/opal/opal-rails","last_synced_at":"2025-05-15T11:03:13.471Z","repository":{"id":45073817,"uuid":"2251603","full_name":"opal/opal-rails","owner":"opal","description":"Bringing Ruby to Rails · Rails bindings for Opal","archived":false,"fork":false,"pushed_at":"2024-07-30T18:42:16.000Z","size":641,"stargazers_count":488,"open_issues_count":3,"forks_count":66,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-10-29T13:50:44.196Z","etag":null,"topics":["opal","opal-rails","rails","rails-bindings","ruby","sprockets"],"latest_commit_sha":null,"homepage":"http://opalrb.com/#getting-started-rails","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/opal.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"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":"2011-08-22T21:56:30.000Z","updated_at":"2024-10-14T16:45:05.000Z","dependencies_parsed_at":"2024-01-13T03:01:29.253Z","dependency_job_id":"5fbafea4-69ac-4ad1-aa66-4e691731e303","html_url":"https://github.com/opal/opal-rails","commit_stats":{"total_commits":514,"total_committers":22,"mean_commits":"23.363636363636363","dds":0.09922178988326846,"last_synced_commit":"d66a14ac5af6b5d0e38e9fafe41a0fc4a6149a8a"},"previous_names":[],"tags_count":50,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opal%2Fopal-rails","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opal%2Fopal-rails/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opal%2Fopal-rails/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opal%2Fopal-rails/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/opal","download_url":"https://codeload.github.com/opal/opal-rails/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248911472,"owners_count":21182095,"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":["opal","opal-rails","rails","rails-bindings","ruby","sprockets"],"created_at":"2024-07-31T13:00:49.273Z","updated_at":"2025-04-14T15:53:22.143Z","avatar_url":"https://github.com/opal.png","language":"Ruby","readme":"# Opal Rails\n\n[![Build Status](https://github.com/opal/opal-rails/actions/workflows/build.yml/badge.svg)](https://github.com/opal/opal-rails/actions/workflows/build.yml)\n[![Maintainability](https://api.codeclimate.com/v1/badges/31dda24adcecb836348f/maintainability)](https://codeclimate.com/github/opal/opal-rails/maintainability)\n[![Gem Version](https://badge.fury.io/rb/opal-rails.svg)](http://badge.fury.io/rb/opal-rails)\n![fun guaranteed](https://img.shields.io/badge/fun-guaranteed-brightgreen.svg)\n![web scale](http://img.shields.io/badge/webscale-over%209000-green.svg)\n\n_Rails bindings for [Opal](http://opalrb.com). ([Changelog](https://github.com/opal/opal-rails/blob/master/CHANGELOG.md))_\n\n### Looking for Webpack support? 👀\n\nIf you want to integrate Opal via Webpack please refer to [opal-webpack-loader](https://github.com/isomorfeus/opal-webpack-loader#installation) installation instructions.\n\nℹ️ 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_.\n\n\n## Installation\n\nIn your `Gemfile`\n\n```ruby\ngem 'opal-rails'\n```\n\nRun `opal:install` Rails generator to add `app/assets/javascript` to your asset-pipeline manifest in `app/assets/config/manifest.js`:\n\n```\nbin/rails g opal:install\n```\n\n\n### Configuration\n\n#### For the compiler\n\nThe following automatically gets added to your configuration for the compiler when running the `opal:install` Rails generator:\n\n`config/initializers/opal.rb`\n\n```ruby\n# Compiler options\nRails.application.configure do\n  config.opal.method_missing_enabled   = true\n  config.opal.const_missing_enabled    = true\n  config.opal.arity_check_enabled      = true\n  config.opal.freezing_stubs_enabled   = true\n  config.opal.dynamic_require_severity = :ignore\nend\n```\n\nCheck out the full list of the available configuration options at: [lib/opal/config.rb](https://github.com/opal/opal/blob/master/lib/opal/config.rb).\n\n#### For template assigns\n\nYou may optionally add configuration for rendering assigns when using the template handler from actions:\n\n`config/initializers/opal.rb`\n\n```ruby\nRails.application.configure do\n  # ...\n  config.opal.assigns_in_templates = true\n  config.opal.assigns_in_templates = :locals # only locals\n  config.opal.assigns_in_templates = :ivars # only instance variables\nend\n```\n\n\nLocal and instance variables will be sent down to the view after converting their values to JSON.\n\n\n## Usage\n\n### Basic example\n\n#### Rails 7 example\n\nThis example assumes Rails 7 and having followed the [Installation](#installation) instructions.\n\n1- Delete `app/javascript/application.js`\n\n2- Enable the following lines in the generated `app/assets/javascript/application.js.rb` below `require \"opal\"`:\n\n```ruby\nputs \"hello world!\"\nrequire \"native\"\n$$[:document].addEventListener :DOMContentLoaded do\n  $$[:document][:body][:innerHTML] = '\u003ch2\u003eHello World!\u003c/h2\u003e'\nend\n```\n\n3- Run `rails g scaffold welcome`\n\n4- Run `rails db:migrate`\n\n5- Clear `app/views/welcomes/index.html.erb` (empty its content)\n\n6- Run `rails s`\n\n7- Visit `http://localhost:3000/welcomes`\n\nIn the browser webpage, you should see:\n\n\u003ch2\u003eHello World!\u003c/h2\u003e\n\nAlso, you should see `hello world!` in the browser console.\n\n#### Rails 5 example\n\nThis example assumes Rails 5.\n\n1. Rename `app/assets/javascripts/application.js` to `app/assets/javascripts/application.js.rb`\n2. Replace the Sprockets directives with plain requires\n\n```ruby\n# Require the opal runtime and core library\nrequire 'opal'\n\n# For Rails 5.1 and above, otherwise use 'opal_ujs'\nrequire 'rails_ujs'\n\n# Require of JS libraries will be forwarded to sprockets as is\nrequire 'turbolinks'\n\n# a Ruby equivalent of the require_tree Sprockets directive is available\nrequire_tree '.'\n\nputs \"hello world!\"\n```\n\n#### A more extensive Rails 5 example\n\n```ruby\nrequire 'opal'\nrequire 'opal_ujs'\nrequire 'turbolinks'\nrequire_tree '.' # a Ruby equivalent of the require_tree Sprockets directive is available\n\n# ---- YOUR FANCY RUBY CODE HERE ----\n#\n# Examples:\n\n# == Print something in the browser's console\nputs \"Hello world!\"\npp hello: :world\nrequire 'console'\n$console.log %w[Hello world!]\n\n# == Use Native to wrap native JS objects, $$ is preconfigured to wrap `window`\nrequire 'native'\n$$.alert \"Hello world!\"\n\n# == Do some DOM manipulation with jQuery\nrequire 'opal-jquery'\nDocument.ready? do\n  Element.find('body').html = '\u003ch1\u003eHello world!\u003c/h1\u003e'\nend\n\n# == Or access the DOM api directly\n$$[:document].addEventListener(:DOMContentLoaded, -\u003e {\n  $$[:document].querySelector('body')[:innerHTML] = '\u003ch1\u003eHello world!\u003c/h1\u003e'\n})\n\n```\n\n\n### Using Sprockets directives and `application.js`\n\nIf 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.:\n\n```js\n//= require opal\n//= require rails_ujs\n//= require turbolinks\n//= require_tree .\n//= require app\n\nOpal.require('opal');\nOpal.require('app');\n```\n\n\n### As a template\n\nYou can use it for your views too:\n\n```ruby\n# app/controllers/posts_controller.rb\n\ndef create\n  @post = Post.create!(params[:post])\n  render type: :js, locals: {comments_html: render_to_string(@post.comments)}\nend\n```\n\nAssigned instance that would normally be available in your views are converted to JSON objects first.\n\n```ruby\n# app/views/posts/create.js.opal\n\npost = Element.find('.post')\npost.find('.title').html    = @post[:title]\npost.find('.body').html     = @post[:body]\npost.find('.comments').html = comments_html\n```\n\n#### Instance and local variables in templates\n\nBy default `opal-rails`, will NOT forward any instance and local variable you'll pass to the template.\n\nThis behavior can be enabled by setting `Rails.application.config.opal.assigns_in_templates` to `true` in `config/initializers/assets.rb`:\n\n```ruby\nRails.application.configure do\n  # ...\n  config.opal.assigns_in_templates = true\n  # ...\nend\n```\n\n\n\n### As a Haml filter (optional)\n\nOf course you need to require `haml-rails` separately since its presence is not assumed\n\n```haml\n-# app/views/posts/show.html.haml\n\n%article.post\n  %h1.title= post.title\n  .body= post.body\n\n%a#show-comments Display Comments!\n\n.comments(style=\"display:none;\")\n  - post.comments.each do |comment|\n    .comment= comment.body\n\n:opal\n  Document.ready? do\n    Element.find('#show-comments').on :click do |click|\n      click.prevent_default\n      click.current_target.hide\n      Element.find('.comments').effect(:fade_in)\n    end\n  end\n```\n\n\n### RSpec support\n\n_Extracted to (unreleased) [`opal-rspec-rails`](https://github.com/opal/opal-rspec-rails)_\n\nAdd this line to your `Gemfile`:\n\n```ruby\ngem 'opal-rspec-rails', github: 'opal/opal-rspec-rails'\n```\n\n\n### Minitest support\n\n_Upcoming as `opal-minitest-rails`_\n\n\n### Shared templates\n\nAs long as the templates are inside the Sprockets/Opal load path, then you should be able to just require them.\n\nLet's say we have this template `app/views/shared/test.haml`:\n\n```haml\n.row\n  .col-sm-12\n    = @bar\n```\n\nWe need to make sure Opal can see and compile that template. So we need to add the path to sprockets:\n\n```ruby\n# config/initializers/opal.rb\nRails.application.config.assets.paths \u003c\u003c Rails.root.join('app', 'views', 'shared').to_s\n```\n\nNow, somewhere in `application.rb` you need to require that template, and you can just run it through `Template`:\n\n```ruby\n# app/assets/javascripts/application.rb\nrequire 'opal'\nrequire 'opal-haml'\nrequire 'test'\n\n@bar = \"hello world\"\n\ntemplate = Template['test']\ntemplate.render(self)\n# =\u003e  '\u003cdiv class=\"row\"\u003e\u003cdiv class=\"col-sm-12\"\u003ehello world\u003c/div\u003e\u003c/div\u003e'\n```\n\n\n### Using Ruby gems from Opal\n\nJust use `Opal.use_gem` in your asset initializer (`config/initializers/assets.rb`).\n\nExample:\n\n```ruby\nOpal.use_gem 'cannonbol'\n```\n\n\n## Contributing\n\nRun the specs:\n\n```\nbin/setup\nbin/rake\n```\n\nInspect the test app:\n\n```\nbin/rackup\n# visit localhost:9292\n```\n\nTinker with a sandbox app:\n\n```\nbin/sandbox # will re-create the app\nbin/rails s # will start the sandbox app server\n# visit localhost:3000\n```\n\n\n## License\n\n© 2012-2024 Elia Schito\n\n    Permission is hereby granted, free of charge, to any person obtaining a copy\n    of this software and associated documentation files (the \"Software\"), to deal\n    in the Software without restriction, including without limitation the rights\n    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n    copies of the Software, and to permit persons to whom the Software is\n    furnished to do so, subject to the following conditions:\n\n    The above copyright notice and this permission notice shall be included in\n    all copies or substantial portions of the Software.\n\n    THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n    THE SOFTWARE.\n","funding_links":[],"categories":["JavaScript","Uncategorized","Ruby"],"sub_categories":["JavaScript Tools","Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopal%2Fopal-rails","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopal%2Fopal-rails","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopal%2Fopal-rails/lists"}