I would like to add new javascript file to my spree extension

923 views Asked by At

I am new in rails. I'm trying to build Spree extension to use Braintree's drop-in UI. I'm trying to add new braintree.js to my extension. These are the steps I did so far.

  1. Added braintree.js to <my_extension>/app/assets/javascripts/spree/frontend/

  2. Added //= require spree/frontend/braintree to app/assets/javascripts/spree/frontend/<my_extension>.js.

  3. Created braintree.rb in <my_extension>/config/initializers/. Content of this file:

    Braintree::Configuration.environment = :sandbox
    Braintree::Configuration.merchant_id = "merchant_id"
    Braintree::Configuration.public_key = "public_key"
    Braintree::Configuration.private_key = "private_key"
    
  4. From my spree store I ran bundle install, which was successful.

  5. Ran rails g <my_extension>:install which gives me the following error:

    append  vendor/assets/javascripts/spree/frontend/all.js
    append  vendor/assets/javascripts/spree/backend/all.js
    insert  vendor/assets/stylesheets/spree/frontend/all.css
    insert  vendor/assets/stylesheets/spree/backend/all.css
    run  bundle exec rake railties:install:migrations FROM=spree_hello_ext 
    
    from "."
    Would you like to run the migrations now? [Y/n] y
    run  bundle exec rake db:migrate from "."
    rake aborted!
    NameError: uninitialized constant Braintree
    /Users/<user>/Sites/spree_hello_ext/config/initializers/braintree.rb:1:in 
       `<top (required)>'
    /Users/<user>/Sites/hellostore/config/environment.rb:5:in `<top (required)>'
    Tasks: TOP => db:migrate => environment
    (See full trace by running task with --trace)
    
1

There are 1 answers

0
JDutil On

You need to require the braintree gem in your gemspec as a dependency, and then require it lib/spree_hello_ext.rb for it to be initialized before your initializer tries to use it. Similar to how spree_core it required by spree_gateway: https://github.com/spree/spree_gateway/blob/master/lib/spree_gateway.rb#L1