Changing Spree from_address - ruby-on-rails

I am trying to change Spree 3.0 from_email
I added this line to my spree initialiser, but it does not work:
Spree::Store.current.mail_from_address = “x#x.com"
Do you know of any reason why not?
I also put it directly in my mailer decorator:
Spree::OrderMailer.class_eval do
def confirm_email_to_store(order, resend = false)
Spree::Store.current.mail_from_address = "x#x.com"
#order = order.respond_to?(:id) ? order : Spree::Order.find(order)
subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')
subject += "#{'Will Call' if #order.will_call} #{'Just to See' if #order.just_to_see} ##{#order.number}"
mail(to: ENV['STORE_EMAIL'], from: from_address, subject: subject)
end
end
This also did not work

Check you might have created multiple stores via checking Spree::Store.all
Also, spree use current store as store which updated last so you have to check that also

You can simply change the from email address in the Admin Panel under Configuration -> General settings:

Got it to work this way:
Spree::Store.current.update(mail_from_address: ENV["STORE_EMAIL"])
Looking here http://www.davidverhasselt.com/set-attributes-in-activerecord/ you can see that:
user.name = "Rob"
This regular assignment is the most common and easiest to use. It is
the default write accessor generated by Rails. The name attribute will
be marked as dirty and the change will not be sent to the database
yet.
Of course, spree initializers claims to do save in the databse, but it did not:
If a preference is set here it will be stored within the cache & database upon initialization.
Finally calling Spree::Store.current will pull from the database, so any unsaved changes will be lost:
scope :by_url, lambda { |url| where("url like ?", "%#{url}%") }
def self.current(domain = nil)
current_store = domain ? Store.by_url(domain).first : nil
current_store || Store.default
end
Fixing this bug in Spree would be prefrable, this is sort of a workaround

Related

Find active record on the bases of any other id

I want to find records on the bases of demo_id from Demojobs model.
def show
#demojob = Demojob.find params[:demo_id] #instead of params[:id]
end
but it shows a error Couldn't find Demojob without an ID
Use .find_by
#demojob = Demojob.find_by(demo_id: params[:demo_id])
EDIT:
For Rails version lower than 4.0.2, use:
#demojob = Demojob.where(demo_id: params[:demo_id]).first

How to access to the company name into an email in rails?

I'm currently trying to use the company name present into an email. But I don't find a simple way to access to. And ask i there is others.
An example is better than explanation :
User.new (mail) => user#company1.com
--> That the value that I want to catch <--
==> #company = company1
if Company.where(name: #company).any?
render show #company
else
reder new Company
end
So if you have any solutions to access to that, you'll be my hero !
Thanks for your time
I'm not an expert on rails, but you can try this :
#pars1 = #company.split('#')
#pars2 = #pars[1].split('.')
#pars3 = #pars[0]
=> company
Try to read that, it can be useful :
How can you return everything after last slash(/) in a Ruby string

How to make my seeds file Idempotent?

In my deployment process I am running my seeds file. I want this to be Idempotent so I can run it multiple times without any issue.
Currently I get PG primary key errors if I run it multiple times.
My seeds pattern looks like this:
user = User.create(.....)
user.save!
foo = Foo.create(....)
foo.save!
How can I make this Idempotent?
Is this the best way?
if( user.exists?(some_column: some_value) )
else
# do insert here
end
I believe you can make use of first_or_create
User.where(email: "email#gmail.com").first_or_create do |user|
user.name = "John"
end
This will only create User with email = "email#gmail.com" if it doesn't exist or it will return you the instance of existing User.
This way you can avoid the Unique Key Violation
You can try :
unless user.find_by(some_column: some_value)
user.save!
end

Spree error when using decorator with the original code

Need a little help over here :-)
I'm trying to extend the Order class using a decorator, but I get an error back, even when I use the exactly same code from source. For example:
order_decorator.rb (the method is exactly like the source, I'm just using a decorator)
Spree::Order.class_eval do
def update_from_params(params, permitted_params, request_env = {})
success = false
#updating_params = params
run_callbacks :updating_from_params do
attributes = #updating_params[:order] ? #updating_params[:order].permit(permitted_params).delete_if { |k,v| v.nil? } : {}
# Set existing card after setting permitted parameters because
# rails would slice parameters containg ruby objects, apparently
existing_card_id = #updating_params[:order] ? #updating_params[:order][:existing_card] : nil
if existing_card_id.present?
credit_card = CreditCard.find existing_card_id
if credit_card.user_id != self.user_id || credit_card.user_id.blank?
raise Core::GatewayError.new Spree.t(:invalid_credit_card)
end
credit_card.verification_value = params[:cvc_confirm] if params[:cvc_confirm].present?
attributes[:payments_attributes].first[:source] = credit_card
attributes[:payments_attributes].first[:payment_method_id] = credit_card.payment_method_id
attributes[:payments_attributes].first.delete :source_attributes
end
if attributes[:payments_attributes]
attributes[:payments_attributes].first[:request_env] = request_env
end
success = self.update_attributes(attributes)
set_shipments_cost if self.shipments.any?
end
#updating_params = nil
success
end
end
When I run this code, spree never finds #updating_params[:order][:existing_card], even when I select an existing card. Because of that, I can never complete the transaction using a pre-existent card and bogus gateway(gives me empty blanks errors instead).
I tried to bind the method in order_decorator.rb using pry and noticed that the [:existing_card] is actuality at #updating_params' level and not at #updating_params[:order]'s level.
When I delete the decorator, the original code just works fine.
Could somebody explain to me what is wrong with my code?
Thanks,
The method you want to redefine is not really the method of the Order class. It is the method that are mixed by Checkout module within the Order class.
You can see it here: https://github.com/spree/spree/blob/master/core/app/models/spree/order/checkout.rb
Try to do what you want this way:
Create file app/models/spree/order/checkout.rb with code
Spree::Order::Checkout.class_eval do
def self.included(klass)
super
klass.class_eval do
def update_from_params(params, permitted_params, request_env = {})
...
...
...
end
end
end
end

Rails - Exclude an attribute from being saved

I have a column named updated_at in postgres. I'm trying to have the db set the time by default. But Rails still executes the query updated_at=NULL. But postgres will only set the timestamp by default when updated_at is not in the query at all.
How do I have Rails exclude a column?
You can disable this behaviour by setting ActiveRecord::Base class variable
record_timestamps to false.
In config/environment.rb, Rails::Initializer.run block :
config.active_record.record_timestamps = false
(if this doesn't work, try instead ActiveRecord::Base.record_timestamps = false at the end of the file)
If you want to set only for a given model :
class Foo < ActiveRecord::Base
self.record_timestamps = false
end
Credit to Jean-François at http://www.ruby-forum.com/topic/72569
I've been running into a similar issue in Rails 2.2.2. As of this version there is an attr_readonly method in ActiveRecord but create doesn't respect it, only update. I don't know if this has been changed in the latest version. I overrode the create method to force is to respect this setting.
def create
if self.id.nil? && connection.prefetch_primary_key?(self.class.table_name)
self.id = connection.next_sequence_value(self.class.sequence_name)
end
quoted_attributes = attributes_with_quotes(true, false)
statement = if quoted_attributes.empty?
connection.empty_insert_statement(self.class.table_name)
else
"INSERT INTO #{self.class.quoted_table_name} " +
"(#{quoted_attributes.keys.join(', ')}) " +
"VALUES(#{quoted_attributes.values.join(', ')})"
end
self.id = connection.insert(statement, "#{self.class.name} Create",
self.class.primary_key, self.id, self.class.sequence_name)
#new_record = false
id
end
The change is just to pass false as the second parameter to attributes_with_quotes, and use quoted_attributes.keys for the column names when building the SQL. This has worked for me. The downside is that by overriding this you will lose before_create and after_create callbacks, and I haven't had time to dig into it enough to figure out why. If anyone cares to expand/improve on this solution or offer a better solution, I'm all ears.

Resources