Rails activestorage not working with shopify_app gem - ruby-on-rails

I am creating a Shopify app, I want to attach the image with the shop model.
but I can not able to start to serve when I add active storage association with the shop model.
error
/home/web/.rvm/gems/ruby-2.6.1/gems/activerecord-6.0.1/lib/active_record/dynamic_matchers.rb:22:in method_missing': undefined method has_one_attached' for Shop (call 'Shop.connection' to establish a connection):Class (NoMethodError)
configuration:
rails:- 6.0.1, ruby:- 2.6.1, shopify_app:- 11.3.2
This is what my code looks like now
shop.rb
class Shop < ActiveRecord::Base
include ShopifyApp::SessionStorage
has_one_attached :icon, dependent: :destroy
end
config/initializers/shopify_app.rb
ShopifyApp.configure do |config|
config.application_name = "App Name"
config.api_key = ENV['shopify_api_key']
config.secret = ENV['shopify_api_secret']
config.old_secret = "<old_secret>"
config.scope = "write_script_tags,read_script_tags,read_themes"
config.embedded_app = false
config.after_authenticate_job = false
config.api_version = ShopifyVersion.shopify_api_version
config.session_repository = Shop
config.webhooks = [
{topic: 'app/uninstalled', address: 'https://example.com/webhooks/app_uninstalled', format: 'json'},
{topic: 'shop/update', address: 'https://example.com/webhooks/shop_update', format: 'json'},
end
I tried this:
config/initializers/shopify_app.rb
config.session_repository = 'ShopifyApp::InMemorySessionStore'
but still, it's not working on the production.
error
ShopifyApp::InMemorySessionStore::EnvironmentError (Cannot use InMemorySessionStore in a Production environment. Please initialize ShopifyApp with a model that can store and retrieve sessions):
also, try to upgrade the gem version but no hope then create a demo with the current configuration and it will work. I can not understand why it will not work on my current project.
can anyone explain it to me?
I really appreciate your suggestions.
sorry for bad English.
Thanks in advance.

After doing google and try to search on the Shopify community. i got some suggestions, and base on suggestion i try to update only the shopify_app gem version and configuration base on the new version.
It works for me.
change 13.2.0 instead of 11.3.2.

Related

Rails 6.1 development environment not printing query trigger code line with verbose active

I have both lines:
# Highlight code that triggered database queries in logs.
config.active_record.verbose_query_logs = true
ActiveRecord::Base.verbose_query_logs = true
in my config/environments/development/rb
But in my development log the sql queries are logged like:
2022-08-10 08:35:18.536410 D [74:puma srv tp 004] (0.006ms) ActiveRecord -- Model Load -- { :sql => "SELECT ....", :binds => { .... }, :allocations => 1, :cached => true }
I have an N+1 queries issue to fix, but the information about which line in the code triggered the query is missing, so this is not helping me much.
I tried also using active-record-query-trace gem, with configuration:
if Rails.env.development?
ActiveRecordQueryTrace.enabled = true
ActiveRecordQueryTrace.level = :full
ActiveRecordQueryTrace.colorize = true # No colorization (default)
ActiveRecordQueryTrace.colorize = :light_purple
# Optional: other gem config options go here
end
but I see no changes at all in how queries are logged.
How can I enable the logging of the line triggering the query?
Thanks
If you want to know the reference point that made a DB query I used to use a gem called Marginalia. I use it in Rails 6 and it does exactaly what you are looking for.
From what I am reading Rails 7 includes this feature as a native feature (which is quite awesome I might add).
I found a link that talks about this:
Rails 7 includes Marginalia
Their example says:
# config/application.rb
module Saeloun
class Application < Rails::Application
config.active_record.query_log_tags_enabled = true
end
end

Rails 5 belongs_to_required_by_default doesn't work

I use Rails 5.0.0, but for some reason belongs_to_required_by_default doesn't work!
Application was created as new rails 5 app
class Visit < ApplicationRecord
belongs_to :user
end
> v = Visit.new
> v.valid? # => true
it works only with optional: false option
class Visit < ApplicationRecord
belongs_to :user, optional: false
end
> v = Visit.new
> v.valid? # => false
but why doesn't work configuration:
Rails.application.config.active_record.belongs_to_required_by_default = true
Where are you putting it? Have confirmed it works by putting it in development.rb as config.active_record.belongs_to_required_by_default = true inside Rails.application.configure do.
If you want it for everything you can put it in application.rb under class Application < Rails::Application as config.active_record.belongs_to_required_by_default = true
I believe you'll find putting it in the initializers directory will have problems with the loading order.
EDIT FOR RAILS 5.1: Everything should work well on a default Rails 5.1 application. Just make sure config.load_defaults 5.1 is in your application.rb (reference).
OLD ANSWER FOR RAILS 5.0.x
It look like this is due to some gems that monkey patch activerecord incorrectly, according to this Rails issue https://github.com/rails/rails/issues/23589.
You may want to comment/uncomment them out in your Gemfile until you find the culprit.
After this tedious process, I found that for my latest project it was the gems ahoy_matey, cancancan and delayed_job_active_record that caused the problem (at the time of writing).
In the meantime Ropeney's answer works, although not ideal since the "official rails way" is to declare config.active_record.belongs_to_required_by_default = true in the new_framework_default‌​s.rb initializer, not in application.rb.
In case anyone is still having this issue, you can upgrade to Rails 5.1 to fix it. In Rails 5.1, config/initializers/new_framework_defaults.rbhas been removed and replaced with the line config.load_defaults 5.1 in application.rb. This line includes
active_record.belongs_to_required_by_default = true and the other options that were in new_framework_defaults.rb.
module myApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails
version.
config.load_defaults 5.1
See the end of this thread for more details: https://github.com/rails/rails/issues/23589.
active_record.belongs_to_required_by_default = true only works for FactoryBot.create() method. you will still get validation error of child records not present where you have used FactoryBot.build() method. Any idea why this is not working for build()? any workaround?

Integrating payu payment gateway (active_merchant_payu_in) with spree

I am trying to integrate payu.in payment gateway with spree into my rails application. I have included gem 'active_merchant_payu_in' in the application.
My app/models/spree/gateway/payu.rb looks like this:
module Spree
class Gateway::Payu < Gateway
def provider_class
ActiveMerchant::Billing::Integrations::PayuIn
end
end
end
In application.rb
config.after_initialize do |app|
app.config.spree.payment_methods += [
Spree::Gateway::Payu
]
end
Development.rb
config.after_initialize do
ActiveMerchant::Billing::Base.mode = :test
$payu_merchant_id = ActiveMerchant::Billing::Integrations::PayuIn.merchant_id = '--ID--'
$payu_secret_key = ActiveMerchant::Billing::Integrations::PayuIn.secret_key = '--Key--'
end
I have enabled Spree::Gateway::Payu from admin credentials. Now when i do a checkout i get below error.
NoMethodError in Spree::CheckoutController#update
undefined method `authorize' for ActiveMerchant::Billing::Integrations::PayuIn:Module
can someone guide me towards right path.
Many Thanks :)
Incase anyone is still facing this problem.
Solution: Edit your payment method "Spree::Gateway::Payu" and set auto_capture? field to true.
Payu does not support authorize method which is called when "auto capture" is set to false, when set to true, "purchase" method is called which is supported by payu.
You can read more about auto capture in Spree's documentation. https://guides.spreecommerce.com/developer/payments.html

Rails 3 Authlogic acts_as_authentic undefined

I'm getting an error that acts_as_authentic is undefined for rails 3, ruby 1.9.2
My Gemfile has: gem 'authlogic'
The command "bundle show authlogic" shows the correct path where authlogic is installed
My acts_as_authentic appears in a controller:
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.login_field = :phone
end
end
Let me know if there's anything else that will be helpful, I'm new to rails so I'm not entirely sure what to post.
Thanks
For some reason I always ask questions on StackOverflow just before I figure it out. I needed to restart rails server to get it to work.

Rails3 - oauth-plugin problem

I'm trying to use oauth-plugin on a Rails application I'm developing, but I keep running into problems.
To make sure I'm not making any mistake, I started an application from scratch (using Rails 3.0.3). Here are the steps I followed:
Create da new rails application (rails.test)
Edited its Gemfile to include:
gem "oauth-plugin", ">=0.4.0.pre1"
gem "oauth", "0.4.4"
Generated oauth-consumer, by running script/rails g oauth_consumer
Edited oauth_consumers.rb to include my keys for Google integration:
:google=>{
:key=>"anonymous",
:secret=>"anonymous",
:scope=>"https://www.google.com/calendar/feeds/",
:options => {
:site => "http://www.google.com",
:request_token_path => "/accounts/OAuthGetRequestToken",
:access_token_path => "/accounts/OAuthGetAccessToken",
:authorize_path=> "/accounts/OAuthAuthorizeToken"
},
}
Edited routes.rb to add the route for oauth_consumer:
resources :oauth_consumers
Edited application_controller.rb to implement the logged_in? method as follows:
def logged_in?
true
end
Now when I access http://localhost:3000/oauth_consumers/google I get the following error:
uninitialized constant GoogleToken
Does anyone know what causes this error and how can I fix it? GoogleToken is a class that should have been auto generated by oauth-plugin, so I can't tell why I'm getting this uninitialized constant error.
The GoogleToken class doesn't get auto-generated unless you pass "google" to the generator like so:
script/rails g oauth_consumer google
or for rails 3:
rails g oauth_consumer google
Also check to ensure the relationship is set up in the user model like so:
has_one :google, :class_name => "GoogleToken", :dependent => :destroy
Did you remember to run bundle install from terminal after editing your Gemfile? Sounds like your Rails app doesn't know about these gems yet.
I have the same problem, i think a solution could be in this:
https://github.com/pelle/oauth-plugin/blob/master/lib/generators/oauth_consumer/USAGE
You need some sort of authentication like restful-authentication plugin, if you uncomment line 27..29 in your oauth_consumers_controller.rb file, you'll jump to next step!

Resources