Creating Shipping Method in spree from backend - ruby-on-rails

I want to create a shipping method in spree from backend, from front end I am able to achieve it. But in backend when I run this
Spree::ShippingMethod.create(name: "Name 1", zone_id: 1).errors.full_messages
I am getting error
["Calculator can't be blank"]
It seems it needs Spree::Calculator to be created in some way. I am beginner in Spree any help will be helpful.

So, you need to specify a Calculator.
Spree comes with some calculators:
Spree::Calculator::DefaultTax
Spree::Calculator::Shipping::FlatRate
You can also create one as explained here and after that you can create a ShippingMethod this way:
Spree::ShippingMethod.create(name: "Name 1", calculator: Spree::Calculator.first) or whatever Calculator you want.

The correct way will be:-
zone = Spree::Zone.find_by_name("North America")
shipping_method = Spree::ShippingMethod.new(name: "Pickup from Market", zone_id: zone.id)
shipping_method.calculator_type = "Spree::Calculator::FlatRate"
shipping_method.save!
In place of calculator_type we can specify any type of calculator.Because I wanted the Flate Rate I used this one.

Related

Using Wikipedia-Client Gem to Update Rails Database

My ruby and Rails is a bit rusty. I have a table in my database called institutes which has some of the columns filled. I want to use the Wikipedia-Client gem to fill some of the others. I want to use the name attribute to find the page on Wikipedia then use page.summary for the description attribute in my table and page.image_urls.first for the picture attribute. At the moment, I'm struggling to work out how I would go about this.
My current code is:
require 'Wikipedia'
Institute.each do |institute|
school = institute.pluck(:name)
page = Wikipedia.find(school)
description = page.summary
picture = page.image_urls.first
Institute.update!(description: description, picture: picture)
end
I'm clearly doing something wrong here to do with the selection and use of the name attribute to find the Wikipedia page, but can't quite work it out. I think even if I were to pluck the name correctly, it wouldn't assign anything to the right id.
If there's also a way to drop the "The" at the beginning of the name in the Wikipedia search if it exists in :name, that would also be helpful as it seems some institutes drop this on Wikipedia.
You can try to use something like this:
#use https://github.com/kenpratt/wikipedia-client
require 'wikipedia'
#select all Institutes through AR model
Institute.all.each do |institute|
#'institute' is an object, so we can get its name by dot operator
school = institute.name
#try to find school as is
#then try to find without 'The'
#and go ahead only if page exists
page = Wikipedia.find(school)
page = Wikipedia.find(school[3..-1].strip) if page.content.nil? and school[0..2].downcase == 'the'
next if page.content.nil?
description = page.summary
picture = page.image_urls.first
#update Institute object
institute.update!(description: description, picture: picture)
end

Using a/an on an I18n message

I have the following error message on my en.yml file:
invalid_transition: "Cannot %{action} a %{state} timesheet"
Problem is that sometimes the state can be approved, other times it can be rejected.
With that, the error can end up being mispelled like "Cannot submit a approved timesheet", instead of "an approved timesheet".
Does Rails provide this flexibility in I18n?
The most simple answer to your question, I think, would be to pass in the entire state with the correct indefinite article together.
There's a question that looks at how to prepend "a" or "an" depending on a given word. A short answer is that there's a gem indefinite_article that does it.
Your translation then becomes:
invalid_transition: "Cannot %{action} %{state_with_article} timesheet"
Then call the I18n.t and pass in "a rejected" or "an approved" as a variable to interpolate.
However, if you want to get your hands a bit dirtier you may be able to use the i18n-inflector gem and it's companion i18n-inflector-rails gem. For many languages the choice is more complicated than in English because of different genders and tenses affecting the choice of indefinite article. (Disclaimer: I've not used either of those gems but they look like they would help you solve the problem).

Create an object based on parameters from another

I'm Using Rails 3.2 and i wish to know a " rails way " to create an object based on parameters coming from another. I have the class StoredItem and the class RequestedItem, they are not related, but during the process to attend the requested item i wish to list all itens and qty stored from a requested part number.
I thought something like
#requested_items = RequestedItem.includes(:item_description).where("item_request_id = ?", params[:id])
#stored_items = StoredItem.where("item_description_id = ?",#requested_items.item_description_id)
Obviously this seems don't work because i have a lot of descriptions id to compare, not only one, so, whats the best and correct way to make it work?
Any help will be appreciated, thank you :)
#stored_items = StoredItem.where("item_description_id IN (?)",#requested_items.map(&:item_description_id))
Or
#stored_items = StoredItem.where(:item_description_id => #requested_items.map(&:item_description_id))

Spree has no access to database from a partial?

I'm sure this has to do with the complexity of Spree's loading.
But my main problem is that Spree can not load a Country. For me it,s Country.find(214). If I check it in my remote console, I find it no worries. All the countries, all the states are there.
But if I try to set Country.find(214) in the controller for states_controller#index, or make a before_load method that does that, or put it in the view itself, it always returns :Error (Couldn't find Country with ID=214).
Crazy, right? I can't think of what to do at this point. If I do Country = Country.first. I can sort of get it to work by just loading an empty template of index.haml. So that means that some sort of country exists that it is tapping into.
Anyone have any theoretical thoughts as to why this is happening? And how I might be able to circumvent it?
Sorry this is a little late, but I stumbled onto your post and figured out what was causing the problem.
Country 214 is USA, and for some reason Spree defaults to that country. Which means that if you don't have USA loaded, you'll hit into that problem.
To get around it, you'll have to manually set up a default country in your initializer:
Spree.config do |config|
Spree::Config.default_country_id = Spree::Country.find_by_name("Singapore").id
end
I hope you've solved this by now though. :)
Its Pretty much easy in Spree2.0.0 should work for every Spree version too.
Spree.config do |config| # Set Country name and Currency like this(Note: you will need to run 'rake db:seed' before this. Change country name in Spree::Country.find_by_name('United Kingdom') replace united kingdom to your desire one)
config.currency = 'EUR'
country = Spree::Country.find_by_name('United Kingdom')
config.default_country_id = country.id if country.present?
# You can also set following options too.
config.site_name = "Teamer Store"
config.override_actionmailer_config = true
config.enable_mail_delivery = true
end

How to write stories / scenarios in BDD ( Behavior Driven Design )

I am about to use BDD (Behavior Driven Design) for the first time and am trying to get used to this different way of approaching a problem.
Can you give some stories / scenarios that you would write for say a simple login application using BDD?
For example, from what I have read, it seems that is good:
When a user enters an invalid userid/password, then display an error
message.
As opposed to:
Validate id and password by searching for a matching record in the
database.
Dan North has some excellent advice on writing stories. "Dan North- What's in a Story?"
I would also take a look at some of the work being done around Cucumber as they have spent a lot of time thinking about how to write these stories in an understandable and executable way.
Dan North's article that _Kevin mentioned is great.
Remember, though, that there are "user stories," which should actually be written or at least collected from the client/users. These are more of the "As a , I want , so that " type stories.
Then there are acceptance criteria, which identify how and when the user story will be said to be satisfied. That is like what you have written in your post: "When x, it should y."
There is a lot of overlap here with what I call "system stories" in my project management system and "specifications" in my tests which specify behaviour that the user may not be aware of, but describe interaction between your classes.
System story: "When the LoginHandler is given a login and password, it should validate the data with a LoginValidator."
Specification:
[TestFixture]
public class When_the_login_handler_is_given_a_login_and_password
{
constant string login = "jdoe";
constant string password = "password";
static LoginValidator loginValidator;
context c = () => loginValidator = an<ILoginValidator>;
because b = () => sut.Validate(login, password);
it should_validate_the_data_with_a_LoginValidator =
() => loginValidator.was_told_to(x => x.DoValidation(login, password));
}
Nevermind the testing syntax, you can see that the specification text itself is embodied in the test class name and method name. Furthermore, the test/spec is actually testing the behaviour of the classes. When tests like this pass for simple user stories, the acceptance criteria has been met.
I found an awesome talk here https://skillsmatter.com/skillscasts/2446-bdd-as-its-meant-to-be-done
(caution, you need to create an account to view the video)

Resources