Trying to use coffeescript in a rails engine - ruby-on-rails

I'm trying to use some coffee script in an engine but as soon as i name a file with:
*.js.coffee or *.coffee
an error is triggered and i can't load the page:
ExecJS::RuntimeError at /mylocation
SyntaxError: unexpected IDENTIFIER
The page won't load. My coffeescript is working in another app (Actually i'm trying to extract the logic from the main app to an engine). When i try using it in the engine it failed if it's a .coffee file.
I have in gemfile of the core app:
gem 'coffee-rails'
In the engine gemspec:
s.add_dependency 'coffee-rails'
If i rename the file to *.js the page loads but of course i see the syntax error in the js console.
Any idea where i should look or what should i do to use coffeescript in my engine ?

"Unexpected identifier" means that you have a variable you're trying to reference that hasn't been declared. Possibly a function which is not within the coffescript callback?
Make sure you pass all the variables you're trying to use.

I find my stupid error, the file i was playing with was and old js file and when i converted it to coffee i forgot to change the comments lines ( // to # )...

Related

Can't use cells from an engine inside Rails application

I created an engine which provides an ui component as a cell. The corresponding gem (criteria_operator-ui_component) contains nearly no code inside the lib folder, because for cells to function properly I had to work inside the assets path. The base file of the gem looks like this:
require 'criteria_operator/ui_component/engine'
require 'cells/rails'
module CriteriaOperator
module UiComponent
# Your code goes here...
end
end
The engine doesn't contain much, either:
module CriteriaOperator
module UiComponent
class Engine < ::Rails::Engine
require 'jquery-rails'
require 'criteria_operator'
isolate_namespace CriteriaOperator::UiComponent
end
end
end
To me, it looks like the gem couldn't even know about the cell, but as far as I know I'm not allowed to include anything from outside the lib folder. Also, testing the cell in the dummy application within the project is working fine.
Now I'm using this engine inside a real Rails application. In the gemfile, I included the following:
gem 'criteria_operator'
gem 'cells' # i added these three, because `bundler list` didn't show me
gem 'cells-rails' # `cells-rails` and `cells-erb` even though they are listed
gem 'cells-erb' # as dependencies for the engine
gem 'criteria_operator-ui_component'
I mounted the routes
mount CriteriaOperator::UiComponent::Engine => '/criteria_operator-ui_component'
and tried using the cell CriteriaOperator::UiComponent::CriteriaEditor like I did in the dummy application. Inside erb:
cell('criteria_operator/ui_component/criteria_editor', #op)
or from code:
include Cell::RailsExtensions::ActionController
def whatever
cell(CriteriaOperator::UiComponent::CriteriaEditor, #op).call()
end
The error is ActionView::Template::Error (uninitialized constant CriteriaOperator::UiComponent::CriteriaEditor).
What am I doing wrong? Am I just missing something when using the engine, or is the engine itself implemented the wrong way? And if that's the case, why does the dummy application work? I'm totally stuck, this is my first time creating a Rails Engine as well as my fist time working with cells...
The full code of the engine (including the dummy application) can be found on GitHub (this isn't supposed to be any advertisement, it's just in case anyone needs additional information).
You're calling CriteriaOperator::UiComponent::CriteriaEditor but that class/module does not seem to exist.
CriteriaOperator::UiComponent::Engine works OK because it's defined in the engine itself.
I'm guessing that your sample application works because it's using the view-based invocation like cell('criteria_operator/ui_component/criteria_editor') which presumably works with the javascript? You can't use the "code" version without defining the cell as a class like this:
https://github.com/trailblazer/cells#cell-class

Local gem class is not found in Rails application

I have written a generator which creates the following ruby file and folder:
app/tests/test.rb
in the test.rb file I have a Test class which looks like this:
class Test < MyCustomModule::MyCustomClass::Base
...
end
Now, I want to use its functionality in one of the show.html.erb files creating new instance like this:
Test.new(...).render(...).html_safe
but I am getting the following error:
uninitialized constant MyCustomModule::MyCustomClass::Base
I have use the following answer to link my gem and my rails application. It seems to work as I am able to use the generator, but the gem module and class are not seen in the rails application.
Could anyone tell how to fix this issue?
I have try to follow the tips posted here but still nothing changed:
Adding config.autoload_paths += Dir["#{config.root}/lib/**/"] in application.rb file
I have created my gem structure looking at CarrierWave gem, so the naming should be correct
I try to disable config.threadsafe! but it is already disabled since config.cache_classes and config.eager_load are set to false in development
DEPRECATION WARNING: config.threadsafe! is deprecated. Rails
applications behave by default as thread safe in production as long as
config.cache_classes and config.eager_load are set to true.
Also, looking at adding-asset-to-your-gems rails documentation, it is said that:
A good example of this is the jquery-rails gem which comes with Rails
as the standard JavaScript library gem. This gem contains an engine
class which inherits from Rails::Engine. By doing this, Rails is
informed that the directory for this gem may contain assets and the
app/assets, lib/assets and vendor/assets directories of this engine
are added to the search path of Sprockets.
So, I have done this, and put my model class file in assets folder, but the result is the same.
The following screenshots demonstrate my real case:
The screenshot below displays my gem file structure
Here you can see how I am loading the gem in my Rails application Gemfile:
gem 'thumbnail_hover_effect', '0.0.3', github: 'thumbnail_hover_effec/thumbnail_hover_effec', branch: 'master'
Then I am using the gem generator a ruby file with a cutstom name in app/thumbnails/test.rb folder with the following code:
class Test < ThumbnailHoverEffect::Image::Base
...
end
and trying to use the Test class gives me uninitialized constant ThumbnailHoverEffect::Image::Base error.
Back in the gem files, these are how the thumbnail_hover_effect file looks like
require 'thumbnail_hover_effect/version'
require 'thumbnail_hover_effect/engine'
require 'thumbnail_hover_effect/image'
module ThumbnailHoverEffect
# Your code goes here...
end
and hoe the image file looks like:
module ThumbnailHoverEffect
#
class Image
...
end
end
From what you've posted here there is no ThumbnailHoverEffect::Image::Base defined. Rails autoloading conventions (which you should not be depending on a gem btw, more on that later) would be looking for this file in thumbnail_hover_effect/image/base.rb, but the directory structure you printed does not have that. Of course you could define the class in thumbnail_hover_effect/image.rb and it would work, but the abridged snippet you posted does not show that. So where is ThumbnailHoverEffect::Image::Base defined?
If it's in thumbnail_hover_effect/image/base.rb then that would indicate the file is not being loaded. You can sanity check this by putting a puts 'loading this stupid file' at the top of thumbnail_hover_effect/image/base.rb. That will allow you to bisect the problem by seeing whether there is a problem with your definition of the class, or whether the problem is with loading the proper files. Debugging is all about bisecting the problem.

Rails bootstrap gem monkeypatching method not working

I'm using the excellent twitter-bootstrap-rails gem. There is a helper within that gem (NavbarHelper) which is used to generate Bootstrap navbars with a Ruby helper. I want to monkey patch the gem such that the dropdown lists won't have carets.
So, I looked into the source and found the relevant method here. All I have to do is override it. I created a new file in config/initializers called navbar.rb with the following content:
NavbarHelper.module_eval do
def name_and_caret(name)
"HELLO WORLD"
end
end
Presumably, all of the dropdown titles then should be rendered as "HELLO WORLD" in my app (as referenced by the gem source). However, this is not occurring, and the gem does not appear to be monkeypatched at all.
I tried placing puts NavbarHelper.methods - Object.methods in the initializers file, and there were no results, which makes me think that Rails is not loading the gem correctly before the initializers. I have also checked and verified that the gem is not using autoload for its helpers.
Edit
What may be complicating this is the fact that my Gemfile includes the gem in the following manner:
gem 'twitter-bootstrap-rails', git: 'git://github.com/seyhunak/twitter-bootstrap-rails.git', branch: 'bootstrap3'
I'm not sure if this specific versioning means the monkeypatching doesn't work.
Edit #2
It seems there is only one version of the gem on my system, so I don't think that's the issue. Also, I have tried placing require 'twitter-bootstrap-rails at the top of the initializers file, with no results.
The problem is that you patch the method on this module but the module already got included at this point. Try to define this in your application_helper.rb
def name_and_caret(name)
super("blub #{name}")
end

How can I peek at intermediary files processed by asset pipeline?

I've got somefile.js.coffee.erb file which is processed by Rails asset pipeline. My ERB code returns some string that cannot be parsed by Coffee which result in SyntaxError exception. I would like to peek into generated somefile.js.coffee file, or in general any intermediary file processed by asset pipeline.
I've tried to examine Sprockets with no luck:
environment = Sprockets::Environment.new
MyApplication::Application.config.assets.paths.each {|p| environment.append_path p}
rerb = environment['somefile.js.coffee.erb']
rerb.source #=> it's already preprocessed
Or to look into \tmp\cache\assets but there are also only preprocessed files, additionaly obscured by fingerprinted name.
Maybe there is a way to hook into asset-pipeline I have no idea how..
Why I need ERB? To generate client-side-model stubs with fields and validations matching Rails model using KnockoutJS (https://github.com/dnagir/knockout-rails extended -> https://github.com/KrzysztofMadejski/knockout-rails).
I am using Rails '~> 3.2.12', sprockets (2.2.2).
Edit: I've ended up injecting erb code in ### comments, to sneak-peak at generated code while coffeescript file is still compiling:
###
<%= somefun() %>
###
Altough I would suggest using #Semyon Perepelitsa answer as it produces coffee script file as it is seen by coffee compiler.
Just remove "coffee" from the file extension temporarily: somefile.js.erb. You will see its intermediate state at /assets/somefile.js as it won't be processed by CoffeeScript.
I wonder if you can put <% binding.pry %> just before the line and mess around till you get it right. Never tried during a compile and don't use coffeescript. In theory, it should work (or is worth a shot) so long as you put gem pry in your Gemfile and run bundle first.

datatables with rails issue

I have included this in my rails:
gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails'
but when I start the server I'm getting this error :
←[31mYou passed :github as an option for gem 'jquery-datatables-rails', but it is invalid.←[0m
I guess its because I'm running it locally.BTW,what will be my path if I'm running it locally?
1 more thing I followed this cast:
http://railscasts.com/episodes/340-datatables?view=asciicast
I have also tried simply this:
gem 'jquery-datatables-rails'
But after bundle install the css and js files are not downloaded and it is giving me file not found error.
Check the cast:
The github option is new in Bundler 1.1. If you don’t have this you’ll
need to pass in the full git path instead.
So try to use
gem 'jquery-datatables-rails', git: 'https://github.com/rweng/jquery-datatables-rails.git'
Difficult to say why your table is hidden without the view and associated javascript code.
Check if you call the dataTable() function on the correct table DOM element.
Also check for any fancy parameters in the call.
If that checks out, post the code for more help.

Resources