I need a checkout process devoid of a delivery and payment step
(working on a store which accept cash on delivery so I need only the
address step.)
I am using Spree 0.10.2
Things I have tried:
In the site_extension.rb added the following state machine
Checkout.state_machines[:state] = StateMachine::Machine.new(Checkout, :initial => 'address') do
after_transition :to => 'complete', :do => :complete_order
before_transition :to => 'complete', :do => :process_payment
event :next do
transition :to => 'complete', :from => 'address'
end
end
The unwanted steps are removed (at least visually) but when I submit
the address it throws up the following error.
IndexError in
CheckoutsController#update
"payment" is an invalid name
Looking at the trace and couple of similar errors later, I decide to
blindly override two methods from checkouts_controller.rb in
site_extension.rb to do nothing (since they deal with payment I
presume)
def clear_payments_if_in_payment_state
end
def object_params
end
Doing this throws validation errors on all the fields of delivery and
billing address.
I vaguely have a notion that I need to override a couple of methods
from checkouts_controller.rb. If this notion is right then what are
those methods.
You need to :
1) Remove the paymenttransition and delivery state transition step.
2) Also overriding the payment_required method .
3) These steps need to be in order_decorator.rb under app/models/spree directory.
checkout_flow do
go_to_state :address
# go_to_state :payment
go_to_state :complete
remove_transition :from => :delivery, :to => :confirm
remove_transition :from => :delivery, :to => :confirm
remove_transition :from => :payment, :to => :confirm
end
#Spree::Order.state_machine.before_transition :to => :payment, :do => :set_order
def set_order
self.create_proposed_shipments
end
def require_email
return false
end
def payment_required?
false
end
I have tested this on spree 2-0-stable with rails 3.2.14.
Sorry, I can't really help you: Customizing the checkout process with Spree 0.10.2 is kind of a nightmare.
But I would recommend to switch to a more recent version of Spree, like the 1.3-stable.
If you switched to that one, it would be as easy as creating an order_decorator.rb file in your_app_folder/app/models/spree with the following code:
Spree::Order.class_eval do
remove_checkout_step :delivery
remove_checkout_step :payment
end
Related
In AASM you can call the may_run? as in the example code in the AASM.
object:
class Job
include AASM
aasm do
state :sleeping, :initial => true
state :running, :cleaning
event :run do
transitions :from => :sleeping, :to => :running
end
event :clean do
transitions :from => :running, :to => :cleaning
end
event :sleep do
transitions :from => [:running, :cleaning], :to => :sleeping
end
end
end
Example
job = Job.new
job.sleeping? # => true
job.may_run? # => true
job.run
job.running? # => true
job.sleeping? # => false
job.may_run? # => false
job.run # => raises AASM::InvalidTransition
How do I create a helper which tests the may_ for the action if I pass the object and the action in as parameters. Essentially I want to add a prefix to the method call using the helper similar to this:
def state_action_url(job, state)
if job.may_state?
#link_to action
else
#render disabled link text
end
end
You can use smth like this:
def state_action_url(job, state)
if job.public_send("may_#{state}?")
#link_to action
else
#render disabled link text
end
end
I'm implementing the completion suggester using the elasticsearch-rails gem. Everything works except update or delete.
For example when I update the title of an article and try to research again, the same title still exist.
I have included Elasticsearch::Model::Callbacks
Model:
require 'elasticsearch/model'
class Article < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def self.suggest(query)
Article.__elasticsearch__.client.suggest(:index => Article.index_name, :body => {
:suggestions => {
:text => query,
:completion => {
:field => 'suggest'
}
}
})
end
settings :index => { :number_of_shards => 1 } do
mappings :dynamic => 'false' do
indexes :title, :type => 'string', :analyzer => 'english'
indexes :suggest, :type => 'completion', :index_analyzer => 'simple', :search_analyzer => 'simple', :payloads => true
end
end
def as_indexed_json(options={})
{
:name => self.title,
:suggest => {
:input => [self.title, self.content],
:output => self.title,
:payload => {
:id => self.id,
:content => self.content
}
}
}
end
end
Controller:
class ArticlesController < ApplicationController
def update
#article = Article.find(params[:id])
if #article.update_attributes(article_params)
render :json => #article
else
render :json => #article.errors
end
end
# ...
end
we had that same problem.
The only way to change the autocompletion data is to call the optimize API.
Optimize will cause a segment merge.
Completion suggesters are stored in their own datastructure calles FST. They are not part of the regular index, so just refreshing would not work.
The datastructure used to store completion suggestions is only created at index time, when a new segment is written. All the old data will be available until a full cleanup happens, which is only guaranteed when segments are merged.
So call optimize:
http://localhost:9200/indexName/_optimize?max_num_segments=number_of_segments
http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/merge-process.html#optimize-api
The smaller the number of segments the more performant completion will be.
And then check again. For me it worked!
Optimizing is slow and I/O heavy, so you cannot run it all the time, but maybe once a day or so...
Good luck!
https://groups.google.com/forum/?fromgroups#!searchin/elasticsearch/completion$20suggester$20delete/elasticsearch/8Rfg4kGV0ps/YG0V9jM7JhcJ
http://www.elasticsearch.org/blog/you-complete-me/
I am trying to create a message plugin for Redmine. I have couple of doubts regarding this
How could I send the email in Redmine plugin? Is it possible to
create the mailer inside the plugin? If so what is the command for
creating mailer?
I can able to see this(call_hook) method almost all the
controller files. What is the use of this method?
Thanks in Advance
There are two ways how to do it:
Create new Mailer and inherit it from redmine mailer and just add new methods as you want
Patch redmine mailer and add method for sending your mail
I used second one in my plugin RedmineCRM, you could download it and check in lib/redmine_contacts/patches/mailer_patch.rb
require 'dispatcher'
module RedmineContacts
module Patches
module MailerPatch
module InstanceMethods
def contacts_note_added(note, parent)
redmine_headers 'X-Project' => note.source.project.identifier,
'X-Notable-Id' => note.source.id,
'X-Note-Id' => note.id
message_id note
if parent
recipients (note.source.watcher_recipients + parent.watcher_recipients).uniq
else
recipients note.source.watcher_recipients
end
subject "[#{note.source.project.name}] - #{parent.name + ' - ' if parent}#{l(:label_note_for)} #{note.source.name}"
body :note => note,
:note_url => url_for(:controller => 'notes', :action => 'show', :note_id => note.id)
render_multipart('note_added', body)
end
def contacts_issue_connected(issue, contact)
redmine_headers 'X-Project' => contact.project.identifier,
'X-Issue-Id' => issue.id,
'X-Contact-Id' => contact.id
message_id contact
recipients contact.watcher_recipients
subject "[#{contact.projects.first.name}] - #{l(:label_issue_for)} #{contact.name}"
body :contact => contact,
:issue => issue,
:contact_url => url_for(:controller => contact.class.name.pluralize.downcase, :action => 'show', :project_id => contact.project, :id => contact.id),
:issue_url => url_for(:controller => "issues", :action => "show", :id => issue)
render_multipart('issue_connected', body)
end
end
def self.included(receiver)
receiver.send :include, InstanceMethods
receiver.class_eval do
unloadable
self.instance_variable_get("#inheritable_attributes")[:view_paths] << RAILS_ROOT + "/vendor/plugins/redmine_contacts/app/views"
end
end
end
end
end
Dispatcher.to_prepare do
unless Mailer.included_modules.include?(RedmineContacts::Patches::MailerPatch)
Mailer.send(:include, RedmineContacts::Patches::MailerPatch)
end
end
I've a got a method in ActiveRecord::User:
def create_user_from_json(user)
#user=User.new(user)
if #user.save!
#user.activate!
end
end
And I'm trying to call it in a plugin's module method. The plugin is json-rpc-1-1. Here is the relevant code:
class ServiceController < ApplicationController
json_rpc_service :name => 'cme', # required
:id => 'urn:uuid:28e54ac0-f1be-11df-889f-0002a5d5c51b', # required
:logger => RAILS_DEFAULT_LOGGER # optional
json_rpc_procedure :name => 'userRegister',
:proc => lambda { |u| ActiveRecord::User.create_user_from_json(u) },
:summary => 'Adds a user to the database',
:params => [{:name => 'newUser', :type => 'object'}],
:return => {:type => 'num'}
end
The problem is the code in the proc. No matter whether I call ActiveRecord::User.create_user_from_json(u) or ::User.create_user_from_json(u) or User.create_user_from_json(u) I just get undefined method 'create_user_from_json'.
What is the proper way to call a User method from the proc?
I think this needs to be a class method instead of an instance method, declare it like this:
def self.create_user_from_json(user)
...
end
There are 2 methods I want to call after every state transition. Right now I'm doing:
aasm_event :nominate_for_publishing, :before => [:set_state_last_updated_by, :set_state_updated_at] do
transitions :to => :under_review, :from => [:work_in_progress]
end
aasm_event :publish, :before => [:set_state_last_updated_by, :set_state_updated_at] do
transitions :to => :published, :from => [:work_in_progress, :under_review], :guard => :is_publishable?
end
aasm_event :unpublish, :before => [:set_state_last_updated_by, :set_state_updated_at] do
transitions :to => :work_in_progress, :from => [:published, :under_review]
end
Obviously this isn't the best approach. I'm duplicating code, and more fundamentally, I'm associating callbacks with specific transitions when they really apply to the state machine as a whole. What's a better way to handle this?
Why not just use dirty attributes to check if the state has been changed on save?
Like so,
class Model > ActiveRecord::Base
before_save :set_state_updates
private
def set_state_updates
if state_changed?
set_state_last_updated_by
set_state_updated_at
end
end
end
Look aasm callbacks. You want to use after_all_transitions method.