Translate the HEADER of RailsCast calendar - ruby-on-rails

HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday].map{ |key| I18n.t(key, scope: "table") }
but the I18n.locale is null so it gives me the default translation.
Even if I set I18n.locale = "ru" in the module CalendarHelper like that:
module CalendarHelper
def calendar(date = Date.today, &block)
Calendar.new(self, date, block).table
end
I18n.locale = "ru"
class Calendar < Struct.new(:view, :date, :callback)
HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday].map{ |key| I18n.t(key, scope: "table") }
START_DAY = :sunday
.......
the I18n.locale is null and I get the default translation.
But if I set I18n.locale = "ru" within the class it works fine.
class Calendar < Struct.new(:view, :date, :callback)
I18n.locale = "ru"
HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday].map{ |key| I18n.t(key, scope: "table") }
START_DAY = :sunday
.......
.........
how can I pass the params[:local] from the view to class Calendar or is there another solution to translate the HEADER?

Here's how I translated the headers. First, comment out the HEADER declaration...
class Calendar < Struct.new(:view, :date, :callback)
#comment out or remove the line below
#HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
START_DAY = :sunday
Then where the header is defined, replace the HEADER.map with I18n.t(:"date.day_names").map This pulls the day names directly from the config/locales/ *.yml files.
def header
content_tag :tr do
I18n.t(:"date.day_names").map { |day| content_tag :th, day }.join.html_safe
end
end
Now just declare the locale from the params in your controller and you should be all set.

Related

undefined method when trying to get the first day of the week - Rails 5

I'm making a calendar, I'm trying to get the first day of the week, as well as the last day of the week. When trying to do this, I get an error in /lib/calencar.rb:
undefined method `beginning_of_month 'for {: date => Wed, 23 Jan 2019}: Hash
This is the code calendar.rb
class Calendar < Struct.new(:view, :date, :callback)
HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
START_DAY = :sunday
delegate :content_tag, to: :view
def table
content_tag :table, class: "calendar table table-bordered table-striped" do
header + week_rows
end
end
def header
content_tag :tr do
HEADER.map { |day| content_tag :th, day }.join.html_safe
end
end
def week_rows
weeks.map do |week|
content_tag :tr do
week.map { |day| day_cell(day) }.join.html_safe
end
end.join.html_safe
end
def day_cell(day)
content_tag :td, view.capture(day, &callback), class: day_classes(day)
end
def day_classes(day)
classes = []
classes << "today" if day == Date.today
classes << "not-month" if day.month != date.month
classes.empty? ? nil : classes.join(" ")
end
def weeks
#first = DateTime.strptime(date, "%B %d, %Y")
first = date.beginning_of_month.beginning_of_week(START_DAY)
last = date.end_of_month.end_of_week(START_DAY)
(first..last).to_a.in_groups_of(7)
end
end
Also, in the index#index controller I have the following code:
class IndexController < ApplicationController
helper CalendarHelper
def index
#date = params[:date] ? Date.parse(params[:date]) : Date.today
end
end
Thank you
undefined method `beginning_of_month 'for {: date => Wed, 23 Jan 2019}: Hash
You are trying to use a beginning_of_month method on a Hash not on a DateTime object, check the assignment of your date variable.

a Struck constant won't allow me to create a calendar

I am trying to create a calendar and have been following a tutorial on YouTube calendar ruby on rails first video. once I inputted the code I had an error,
uninitialized constant CalenderHelper::Struck
the code is;
module CalenderHelper
def calender(date = Date.today, &block)
Calender.new(self, date, block).table
end
**class Calender < Struck.new(:view, :date, :callback)**
HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
START_DAY = :sunday
delegate :content_tag, to: :view
def table
content_tag :table, class: "calender" do
header + week_rows
end
end
def header
content_tag :tr do
HEADER.map { |day| content_tag :th, day }.join.html_safe
end
end
def week_rows
week.map do |week|
content_tag :tr do
week.map { |day| day_cell(day) }.join.html_safe
end
end.join.html_safe
end
def day_cell(day)
content_tag :td, view.capture(day, &callback), class: day_classes(day)
end
def day_classes(day)
classes = []
classes << "today" if day == Date.today
classes << "notmonth" if day.month != date.month
classes.empty? ? nil : classes.join(" ")
end
def weeks
first = date.beginning_of_month.beginning_of_week(START_DAY)
last = date.end_of_month.end_of_week(START_DAY)
(first..last).to_a.in_groups_of(7)
end
end
end
line with the error is marker with starts on each side
You have a small spelling mistake:
You want to call the Struct class, not Struck.
your line should be:
class Calendar < Struct.new(:view, :date, :callback)
# the rest of the code
end
Hope that helps

Compare an array of dates to one day (in a rails calendar)

I'm using the calendar helper from Railscast: http://railscasts.com/episodes/213-calendars-revised but I'm running into a problem (calendar helper below):
module CalendarHelper
def calendar(date = Date.today, &block)
Calendar.new(self, date, start_date, end_date, scheduled, block).table
end
class Calendar < Struct.new(:view, :date, :start_date, :end_date, :scheduled, :callback)
HEADER = %w[S M T W T F S]
START_DAY = :sunday
delegate :content_tag, to: :view
def table
content_tag :table, class: "calendar" do
header + week_rows
end
end
def header
content_tag :tr do
HEADER.map { |day| content_tag :th, day }.join.html_safe
end
end
def week_rows
weeks.map do |week|
content_tag :tr do
week.map { |day| day_cell(day) }.join.html_safe
end
end.join.html_safe
end
def day_cell(day)
content_tag :td, view.capture(day, &callback), class: day_classes(day)
end
def day_classes(day)
classes = []
classes << "today" if day == Date.today
classes << "start_date" if day == start_date
classes << "end_date" if day == end_date
classes << "notmonth" if day.month != date.month
classes << "scheduled" if day == scheduled
classes.empty? ? nil : classes.join(" ")
end
def weeks
first = date.beginning_of_month.beginning_of_week(START_DAY)
last = date.end_of_month.end_of_week(START_DAY)
(first..last).to_a.in_groups_of(7)
end
end
end
My application spits out an array of scheduled dates (using the ice_cube gem). For each one of those dates I want to match them with dates from the calendar, assigning them with the class "scheduled". I can't figure out how to do this. This code is what I'm trying to make work:
classes << "scheduled" if day == scheduled
'scheduled' comes from the controller:
Application_Controller.rb
def scheduled
Schedule.find(params[:id]).itinerary.all_occurrences if params[:id]
end
helper_method :scheduled
Which returns the following array of dates:
=> [2014-05-16 00:00:00 -0400, 2014-05-19 00:00:00 -0400, 2014-05-20 00:00:00 -0400, 2014-05-21 00:00:00 -0400, 2014-05-22 00:00:00 -0400, 2014-05-23 00:00:00 -0400, 2014-05-26 00:00:00 -0400, 2014-05-27 00:00:00 -0400, 2014-05-28 00:00:00 -0400, 2014-05-29 00:00:00 -0400]
I've tried a number of scenarios but I can't figure it out.
As an example, this will work and show the "scheduled" class for these 3 days but I can't figure out how to loop through all the scheduled dates and still have an || operator in the block:
def day_classes(day)
...
classes << "scheduled" if Date.yesterday == day || Date.tomorrow == day || Date.today == day
...
end
Or maybe someone has a better idea?
After a bit of reading through the ice_cube gem I found a handy method that does exactly what I needed.
def day_classes(day)
...
classes << "scheduled" if scheduled.itinerary.occurs_on?(day)
...
end

loop not work in haml, but work in html.erb

It's very annoying,
Show nothing
- calendar #date do |date|
= date.day
But output of haml is in my expectation
<%= calendar #date do |date| %>
<%= date.day %>
<% end %>
This is my helper source code.
module CalendarHelper
require 'pry'
def widget
concat link_to("Hello", '')
concat " "
concat link_to("Bye", '')
end
def calendar(date = Date.today, &block)
cal_tbl = Calendar.new(self, date, block).table
# content_tag :div do
# cal_tbl
# end
# return cal_tbl
end
class Calendar < Struct.new(:view, :date, :callback)
HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
START_DAY = :sunday
delegate :content_tag, to: :view
def table
content_tag :table, class: "calendar" do
header + week_rows
end
end
def header
content_tag :tr do
HEADER.map { |day| content_tag :th, day }.join.html_safe
end
end
def week_rows
weeks.map do |week|
content_tag :tr do
week.map { |day| day_cell(day) }.join.html_safe
end
end.join.html_safe
end
def day_cell(day)
content_tag :td, view.capture(day, &callback), class: day_classes(day)
end
def day_classes(day)
classes = []
classes << "today" if day == Date.today
classes << "notmonth" if day.month != date.month
classes.empty? ? nil : classes.join(" ")
end
def weeks
first = date.beginning_of_month.beginning_of_week(START_DAY)
last = date.end_of_month.end_of_week(START_DAY)
(first..last).to_a.in_groups_of(7)
end
end
end
As per the haml Git,
A hyphen(-), will run the code but not output the result
An equals sign(=), will output the result of the code
so it should be
= calendar #date do |date|
= date.day
Do this:
- calendar #date do |date|
= date.day
Well HAML to HTML translation follows this convention. the rails code that is supposed to be running and not shown is started with a - character, it is equivalent to <% rails code%> in HTML
= in HAML is equivalent of HTML <%= rails code %>, the closing <%end%> is auto generated in response to do rails code.
do this
= calendar #date do |date|
= date.day

Changing a select input to a checkbox acting as an on/off toggle switch in Rails

I have a set of 7 dropdown inputs allowing admins to say whether they are open or closed for business on a given day. I'd like that changed to 7 open/closed switches (presumably styled checkboxes?) but can't figure out how to do this!
Here are the relevant bits of code I currently have (prior to any change):
app/view/backend/inventory_pool/edit.html.haml
- content_for :title, #inventory_pool
= form_for [:backend, #inventory_pool], html: {name: "form"} do |f|
.content
- if is_admin?
%a.button{:href => root_path}= _("Cancel")
%button.button{:type => :submit}= _("Save %s") % _("Inventory Pool")
%section
%h2= _("Basic Information")
.inner
.field.text
.key
%h3= "#{_("Print Contracts")}"
%p.description
.value
.input
%input{type: "checkbox", name: "inventory_pool[print_contracts]", checked: #inventory_pool.print_contracts}
%section#workdays
%h2= _("Workdays")
.inner
- [1,2,3,4,5,6,0].each do |i|
.field.text
.key
%h3= "#{I18n.t('date.day_names')[i]}"
.value
.input
%select{:name => "store[workday_attributes][workdays][]"}
%option{:label => _("Open"), :value => Workday::WORKDAYS[i]}= _("Open")
%option{:label => _("Closed"), :value => "", :selected => #store.workday.closed_days.include?(i) ? true : nil}= _("Closed")
app/models/workday.rb
class Workday < ActiveRecord::Base
belongs_to :inventory_pool
WORKDAYS = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
def is_open_on?(date)
return false if date.nil?
case date.wday
when 1 return monday
when 2 return tuesday
when 3 return wednesday
when 4 return thursday
when 5 return friday
when 6 return saturday
when 0 return sunday
else
return false #Should not be reached
end
end
def closed_days
days = []
days << 0 unless sunday
days << 1 unless monday
days << 2 unless tuesday
days << 3 unless wednesday
days << 4 unless thursday
days << 5 unless friday
days << 6 unless saturday
days
end
def workdays=(wdays)
WORKDAYS.each {|workday| write_attribute(workday, wdays.include?(workday) ? true : false)}
end
end
And in app/controllers/backend/inventory_pools_controller I have this (abridged):
def update
#inventory_pool ||= InventoryPool.find(params[:id])
process_params params[:inventory_pool]
end
def process_params ip
ip[:print_contracts] ||= "false" # unchecked checkboxes are *not* being sent
ip[:workday_attributes][:workdays].delete "" if ip[:workday_attributes]
end
This may be a pretty heavy change, but I've found the best way to do it is very similar to Ryan Bates' Railscast on using a bitmask. This tutorial is awesome and is relatively quick to set up, and should map very easily from user roles (which is used in the tutorial) to days of the week on a Workday in your app.
OK. In the haml, I had to replace:
.input
%select{:name => "store[workday_attributes][workdays][]"}
%option{:label => _("Open"), :value => Workday::WORKDAYS[i]}= _("Open")
%option{:label => _("Closed"), :value => "", :selected => #store.workday.closed_days.include?(i) ? true : nil}= _("Closed")
With:
= check_box_tag "inventory_pool[workday_attributes][workdays][]", Workday::WORKDAYS[i], #inventory_pool.workday.closed_days.include?(i) ? nil : true

Resources