Search code examples
rubyruby-on-rails-3redminebitnami

adding event functionality to my redmine calendar using bitnami stack


I want to add, update and delete event on my redmine calendar(almost similar to a google calendar).I have installed redmine on a Bitnami stack. But I'm not able to understand the code structure as I do not have proper documentation. Is there anyone who can help me on this. The below listed code is my calendar controller .

       class CalendarsController < ApplicationController
      menu_item :calendar
       before_action :find_optional_project

      rescue_from Query::StatementInvalid, :with => :query_statement_invalid

      helper :issues
      helper :projects
      helper :queries
      include QueriesHelper

      def show
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 ||= User.current.today.year
@month ||= User.current.today.month

@calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month)
retrieve_query
@query.group_by = nil
@query.sort_criteria = nil
if @query.valid?
  events = []
  events += @query.issues(:include => [:tracker, :assigned_to, :priority],
                          :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt]
                          )
  events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt])

  @calendar.events = events
end

render :action => 'show', :layout => false if request.xhr?


end
end

The below is the calender_helper.rb

        module CalendarsHelper
  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) = &#171;
    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) = &#187;
    link_to_month((name + " \xc2\xbb"), target_year, target_month, options)
  end

  def link_to_month(link_name, year, month, options={})
    link_to(link_name, {:params => request.query_parameters.merge(:year => year, :month => month)}, options)
  end
end

Below is the view ,show.html.erb

<h2><%= @query.new_record? ? l(:label_calendar) : @query.name %></h2>

<%= form_tag({:controller => 'calendars', :action => 'show', :project_id => @project},
             :method => :get, :id => 'query_form') do %>
<%= hidden_field_tag 'set_filter', '1' %>
<fieldset id="filters" class="collapsible <%= @query.new_record? ? "" : "collapsed" %>">
  <legend onclick="toggleFieldset(this);"><%= l(:label_filter_plural) %></legend>
  <div style="<%= @query.new_record? ? "" : "display: none;" %>">
    <%= render :partial => 'queries/filters', :locals => {:query => @query} %>
  </div>
</fieldset>

<p style="float:right;">
  <%= link_to_previous_month(@year, @month, :accesskey => accesskey(:previous)) %> | <%= link_to_next_month(@year, @month, :accesskey => accesskey(:next)) %>
</p>

<p class="buttons">
<%= label_tag('month', l(:label_month)) %>
<%= select_month(@month, :prefix => "month", :discard_type => true) %>
<%= label_tag('year', l(:label_year)) %>
<%= select_year(@year, :prefix => "year", :discard_type => true) %>

<%= link_to_function l(:button_apply), '$("#query_form").submit()', :class => 'icon icon-checked' %>
<%= link_to l(:button_clear), { :project_id => @project, :set_filter => 1 }, :class => 'icon icon-reload' %>
</p>
<% end %>

<%= error_messages_for 'query' %>
<% if @query.valid? %>
<%= render :partial => 'common/calendar', :locals => {:calendar => @calendar} %>

<%= call_hook(:view_calendars_show_bottom, :year => @year, :month => @month, :project => @project, :query => @query) %>

<p class="legend cal">
  <span class="starting"><%= l(:text_tip_issue_begin_day) %></span>
  <span class="ending"><%= l(:text_tip_issue_end_day) %></span>
  <span class="starting ending"><%= l(:text_tip_issue_begin_end_day) %></span>
</p>
<% end %>

<% content_for :sidebar do %>
    <%= render :partial => 'issues/sidebar' %>
<% end %>

<% html_title(l(:label_calendar)) -%>

This is the only code related to calendar in routes.rb

      get '/projects/:project_id/issues/calendar', :to => 'calendars#show', :as => 'project_calendar'
      get '/issues/calendar', :to => 'calendars#show'

Solution

  • In this controller you have only show action (HTTP GET). If you want update and delete you are supposed to add methods and logic for them gere. You will also need routes in config/routes.rb.

    I would also recommend you to take a look into other controllers since this one looks more like a renderer of the calendar and nothing more. I don't see create action as well (HTTP POST) which means you create the entity somewhere else and update and delete are supposed to be there as well (if they are not defined already).