Redmineのカレンダー流用まとめ

redmine203を導入してから必要な物をコピペと改造でなんとか使う方法のまとめです。

 

i18n.rb

redmine203/lib/redmine/i18n.rb を hoge/lib/redmine/i18n.rb へ丸々コピー

application_helper.rb

redmin203/app/helpers/calendars_helper.rb から一部抜粋して hoge/app/helpers/application_helper.rb に追記

#encoding: utf-8

module ApplicationHelper
include Redmine::I18n
  def link_to_previous_month(year, month, options={})
    target_year, target_month = if month == 1
                                  [year - 1, 12]
                                else
                                  [year, month - 1]
                                end
    name = if target_month == 12
             "#{month_name(target_month)} #{target_year}"
           else
             "#{month_name(target_month)}"
           end

    # \xc2\xab(utf-8) = «
    link_to_month(("\xc2\xab " + name), target_year, target_month, options)
  end

  def link_to_next_month(year, month, options={})
    target_year, target_month = if month == 12
                                  [year + 1, 1]
                                else
                                  [year, month + 1]
                                end

    name = if target_month == 1
             "#{month_name(target_month)} #{target_year}"
           else
             "#{month_name(target_month)}"
           end

    # \xc2\xbb(utf-8) = »
    link_to_month((name + " \xc2\xbb"), target_year, target_month, options)
  end

  def link_to_month(link_name, year, month, options={})
    link_to_content_update(h(link_name), params.merge(:year => year, :month => month))
  end


private
  def link_to_content_update(text, url_params = {}, html_options = {})
    link_to(text, url_params, html_options)
  end
end

 

モデル毎の専用ヘルパー

redmine203/lib/redmine/helpers/calendar.rb を hoge/lib/redmine/helpers/calendar_miku.rbにコピー
※モデル毎に作る必要がある

# Redmine - project management software
# Copyright (C) 2006-2012  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

module Redmine
  module Helpers

    # Simple class to compute the start and end dates of a calendar
    class CalendarIftb
      include Redmine::I18n
      attr_reader :startdt, :enddt

      def initialize(date, lang = current_language, period = :month)
        @date = date
        @events = []
        @ending_events_by_days = {}
        @starting_events_by_days = {}
        set_language_if_valid lang
        case period
        when :month
          @startdt = Date.civil(date.year, date.month, 1)
          @enddt = (@startdt >> 1)-1
          # starts from the first day of the week
          @startdt = @startdt - (@startdt.cwday - first_wday)%7
          # ends on the last day of the week
          @enddt = @enddt + (last_wday - @enddt.cwday)%7
        when :week
          @startdt = date - (date.cwday - first_wday)%7
          @enddt = date + (last_wday - date.cwday)%7
        else
          raise 'Invalid period'
        end
      end

      # Sets calendar events
      def events=(events)
        @events = events
        @ending_events_by_days = @events.group_by {|event| event.due_date}
        @starting_events_by_days = @events.group_by {|event| event.start_date}
      end
      def events_iftb=(events)
        @events = events
#モデルのカラム名を指定する必要があるところ
        @ending_events_by_days = events.group_by {|event| event.dtend.to_s(:date)}
        @starting_events_by_days = events.group_by {|event| event.dtstart.to_s(:date)}
      end
      
      # Returns events for the given day
      def events_on(day)
         ((@ending_events_by_days[day] || []) + (@starting_events_by_days[day] || [])).uniq
      end

      # Calendar current month
      def month
        @date.month
      end

      # Return the first day of week
      # 1 = Monday ... 7 = Sunday
      def first_wday
        @first_dow ||= (1 - 1)%7 + 1
#無条件に週の始まりは月曜日とするから、こんな書き方で良いはず。
# @first_dow = 1
=begin
        case Setting.start_of_week.to_i
        when 1
          @first_dow ||= (1 - 1)%7 + 1
        when 6
          @first_dow ||= (6 - 1)%7 + 1
        when 7
          @first_dow ||= (7 - 1)%7 + 1
        else
          @first_dow ||= (l(:general_first_day_of_week).to_i - 1)%7 + 1
        end
=end
      end

      def last_wday
        @last_dow ||= (first_wday + 5)%7 + 1
      end
    end
  end
end

 

miku_controller.rb

アプリケーションのコントローラー、hoge/app/controllers/miku_controller.rbに追記

  def calendar
    if params[:year] and params[:year].to_i > 1900
      @year = params[:year].to_i
      if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
        @month = params[:month].to_i
      end
    end

    @year ||= Date.today.year
    @month ||= Date.today.month
    @calendar = Redmine::Helpers::CalendarMiku.new(Date.civil(@year, @month, 1), "ja", :month)
    
    events = Miku.find(:all, :conditions=>["book_day between ? AND ?",@calendar.startdt.to_date.to_s, @calendar.enddt.to_date.to_s])
    @calendar.events_on = events
  end

calendar.html.erb

hoge/app/views/miku/calendar.html.erb
selectを変更するだけで変更されるナイス仕様

<%= form_tag :action => "calendar" do %>
<div class="calendar">
<div class="navi">
	<%= link_to_previous_month(@year, @month) %> 
	<%= select_year(@year, {:prefix => "year", :discard_type => true}, {:onchange => 'this.form.submit()'}) %>
	<%= select_month(@month, {:prefix => "month", :discard_type => true}, {:onchange => "this.form.submit()"})%>
	<%= link_to_next_month(@year, @month) %>
</div>

<% unless @calendar.nil? %>
<%= render :partial => 'calendar', :locals => {:calendar => @calendar} %>
<% end %>
</div>
<% end %>

 

_calendar.html.erb

hoge/app/views/miku/_calendar.html.erbテンプレートファイル

<table class="cal">
<thead>
<tr><th scope="col" title="<%= l(:label_week) %>" class="week-number"></th><% 7.times do |i| %><th scope="col"><%= day_name( (calendar.first_wday+i)%7 ) %></th><% end %></tr>
</thead>
<tbody>
<tr>
<% day = calendar.startdt
while day <= calendar.enddt %>
<%= ("<td class='week-number' title='#{ l(:label_week) }'>#{(day+(11-day.cwday)%7).cweek}</td>".html_safe) if day.cwday == calendar.first_wday %>
<td class="<%= day.month==calendar.month ? 'even' : 'odd' %>
	<%= ' weekend' if day.cwday==6 || day.cwday==7 %>
	<%= ' today' if Date.today == day %>">
<p class="day-num"><%= day.day %></p>
<% calendar.events_on(day.to_s).each do |i| %>
<%== calendar_link_miku(i) %>
  
<% end %>
</td>
<%= '</tr><tr>'.html_safe if day.cwday==calendar.last_wday and day!=calendar.enddt %>
<% day = day + 1
end %>
</tr>
</tbody>
</table>

miku_helper.rb

hoge/app/helpers/miku_helper.rbに追記

module MikuHelper
 def calendar_link_miku(m)
 rtn = link_to m.name, :action=>:show, :id=>m.id
 end
end

 

calendar.css.scss

redmine203/public/stylesheets/calendar.css を hoge/app/assets/stylesheets/calendarcss.scss へ丸々コピー

これでカレンダーが動いているはず!