1

I'm trying to save a recurring_select to a serialized attribute to handle recurring events on a rails app. Using Jayson's post I manage to get the schedule saved but now I can't get the saved attribute shown in index view or update the recurring_select in _form view

This is my model

class Todo < ActiveRecord::Base

  attr_accessible :item, :completed, :schedule, :start_date
  after_initialize :default_values

  validates :item, presence: true
  belongs_to :list 

  belongs_to :tasklib,
             :foreign_key=>"item"

  #recuring model 
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :schedule

  serialize :schedule, Hash

  def schedule=(new_schedule)
     write_attribute(:schedule,RecurringSelect.dirty_hash_to_rule(new_schedule).to_hash)
  end

  def converted_schedule
    the_schedule = Schedule.new(self.start_date)
    the_schedule.add_recurrence_rule(RecurringSelect.dirty_hash_to_rule(self.schedule))
    the_schedule
  end

end

This is my index view :

h1><%= @list.name %></h1>
<table class="table table-striped">
  <thead>
    <tr>    
      <th><%= model_class.human_attribute_name(:item) %></th>
      <th><%= model_class.human_attribute_name(:start_date) %></th>      
      <th><%= model_class.human_attribute_name(:schedule) %></th>      
      <th><%=t '.actions', :default => t("helpers.actions") %></th>
    </tr>
  </thead>
  <tbody>
    <% @list.todos.each do |todo| %>
      <tr>        
        <td><%= todo.item %></td>
        <td><%= todo.start_date %></td>        
        <td><%= todo.schedule %></td>

        <td>
          <%= link_to t('.edit', :default => t("helpers.links.edit")),
                      edit_list_todo_path(@list, todo), :class => 'btn btn-mini' %>
          <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
                      list_todo_path(@list, todo),
                      :method => :delete,
                      :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
                      :class => 'btn btn-mini btn-danger' %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

and this is my _form view:

<%= simple_form_for [@list, if @todo.nil? then @list.todos.build else @todo end], :html => { :class => 'form-horizontal' } do |f| %>

  <%-# f.input :item, input_html: {class: "span6", rows: 3}  -%>

  <%= f.collection_select :item, Tasklib.order(:name),:name,:name, include_blank: true %>

  <%= f.label :start_date, "date" %>
  <%= f.input :start_date %>

  <%= f.label :schedule %>
  <%= f.select_recurring :schedule, nil, :allow_blank => true %>

  <div class="form-actions">
    <%= f.submit 'Save', :class => 'btn btn-primary' %>
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                lists_path, :class => 'btn' %>
  </div>
<% end %>
Community
  • 1
  • 1
dev.sead
  • 81
  • 5

1 Answers1

2

OK I found it ! The model should be :

  def schedule=(new_schedule)
     if new_schedule == nil 
       new_schedule = IceCube::Schedule.new( self.start_date )
     end  

     write_attribute(:schedule, RecurringSelect.dirty_hash_to_rule(new_schedule).to_hash)
  end


  def converted_schedule
     if !self.read_attribute(:schedule).empty?
     the_schedule = IceCube::Schedule.new( self.start_date )
     the_rule    = RecurringSelect.dirty_hash_to_rule( self.read_attribute(:schedule) )
     if RecurringSelect.is_valid_rule?(the_rule)
       the_schedule.add_recurrence_rule( the_rule)
     end
     the_schedule
    end

  end

and the form view should only set a new recurring select like:

  <%= f.select_recurring :schedule, [ ["No recurring", "{}"] ], :allow_blank => true %>
dev.sead
  • 81
  • 5