people said to me that I have an wrong code at this point. The main idea of this method is to create 1 event if user pressed once, and a row of events if user pressed daily or weekly. Everything works well, but code is so bulky.
def create
@event = Event.new(event_params
@event.start_time = DateTime.parse(params[:start_time], "%Y-%m-%d %H:%i")
@event.end_time = DateTime.parse(params[:end_time], "%Y-%m-%d %H:%i")
@event.user_id = current_user.id
@event.update_attributes(:repeat_id => @event.id) if @event.save
respond_to do |format|
if @event.save
format.html { redirect_to persons_profile_path }
else
format.html { render :new }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
interval = 60 if @event.repeat =='daily'
interval = 20 if @event.repeat =='weekly'
#creating row of events
if @event.repeat != 'once'
(1..interval).each do |i|
@event = Event.new(event_params)
@event.start_time = DateTime.parse(params[:start_time], "%Y-%m-%d %H:%i")
@event.end_time = DateTime.parse(params[:end_time], "%Y-%m-%d %H:%i")
@event.user_id = current_user.id
if @event.repeat =='daily'
@event.start_time = DateTime.parse(@event.start_time.to_s) + i.day
@event.end_time = DateTime.parse(@event.end_time.to_s) + i.day
end
if @event.repeat =='weekly'
@event.start_time = DateTime.parse(@event.start_time.to_s) + i.week
@event.end_time = DateTime.parse(@event.end_time.to_s) + i.week
end
@event.update_attributes(:repeat_id => k) if @event.save
@event.save
end
end
I can't find another solutions. Any ideas? Dont pay attention on parse methods on date, its needed to JS.
)on the second line. My main question is, does the code not work or is the issue just that it is written inefficiently? If it is that later this should be moved to code review, if its the former you need to add more details.DateTime.parseon a particular piece of data once, then re-use that value. You can also collapse yourifinto acaseto figure out what offset to use, then add that offset on to both values.