Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class NextAction < ActiveRecord::Base
- #with (modelFor('Action')) {
- # belongsTo('Context');
- # belongsTo('Project');
- # validatesPresenceOf('name');
- #}
- belongs_to :context
- belongs_to :project
- validates_presence_of :name
- acts_as_list :scope => :context_id
- #
- #Action.findPending = function(conditions) {
- # if (conditions != null)
- # conditions = ' AND ' + conditions;
- # else
- # conditions = '';
- # return Action.findBySql("SELECT Action.*, Context.name AS cname" +
- # " FROM Action LEFT OUTER JOIN Context" +
- # " ON Action.contextId = Context.id" +
- # " WHERE Action.completedAt IS NULL" + conditions +
- # " ORDER BY cname ASC," +
- # " Action.position ASC, Action.name ASC");
- #}
- def find_pending(conditions='')
- if conditions
- find_all_by_completed_at nil, :conditions => conditions, :order => "Context.name ASC,position ASC,name ASC"
- else
- find_all_by_completed_at nil,:order=>"Context.name ASC, position ASC, name ASC"
- end #endif
- end #end find_pending
- #Action.findCompleted = function() {
- # return Action.findBySql("SELECT Action.*, Context.name AS cname" +
- # " FROM Action LEFT OUTER JOIN Context" +
- # " ON Action.contextId = Context.id" +
- # " WHERE Action.completedAt IS NOT NULL" +
- # " ORDER BY Action.completedAt DESC, Action.name ASC")
- #}
- #
- def find_completed()
- find_all_by_completed_at !nil, :order => "completed_at DESC, name ASC"
- end #end find_completed
- #// Repeating actions encoding:
- #// code
- #// num of args
- #// description
- #// example
- #// w: 1, next day-of-week: Every _Sunday_
- #// m: 1, next day-of-month: On the _10th_ day of every month
- #// n: 2, nth day-of-week: On the _1st_ _Sunday_ of each month
- #// y: 2, day-of-year: On _1_/_20_ of each year (mm/dd)
- #// a: 1, add-days: _14_ days after each time the task is completed
- #//
- #Action.repeatType = {
- # '' : { // Do not repeat.
- # numArgs : 0,
- # toSummary : function() {
- # return '';
- # },
- # calcNextDate : function(info, date) {
- # return null;
- # }
- # },
- # 'w' : { // Next day-of-week: Every _Sunday_.
- # numArgs : 1,
- # toSummary : function(type, w1) {
- # return 'every ' + dayOfWeekChoices[w1][0];
- # },
- # calcNextDate : function(info, date) {
- # date.setDate(date.getDate() + (7 - date.getDay()) + info.w1);
- # return date;
- # }
- # },
- # 'm' : { // Next day-of-month: On the _10th_ day of every month.
- # numArgs : 1,
- # toSummary : function(type, m1) {
- # return 'monthly, on day ' + m1;
- # },
- # calcNextDate : function(info, date) {
- # date.setMonth(date.getMonth() + 1);
- # date.setDate(info.m1);
- # return date;
- # }
- # },
- # 'n' : { // Nth day-of-week: On the _1st_ _Sunday_ of each month.
- # numArgs : 2,
- # toSummary : function(type, n1, n2) {
- # return 'monthly, on the ' + weekOfMonthChoices[n1 - 1][0] + ' ' + dayOfWeekChoices[n2][0];
- # },
- # calcNextDate : function(info, date) {
- # date.setMonth(date.getMonth() + 1);
- # date.setDate(1);
- # var dayOfWeek = info.n2;
- # if (dayOfWeek < date.getDay())
- # dayOfWeek = dayOfWeek + 7;
- # date.setDate(date.getDate() + dayOfWeek - date.getDay());
- # date.setDate(date.getDate() + (7 * (info.n1 - 1)));
- # return date;
- # }
- # },
- # 'y' : { // Day-of-year: On _1_/_20_ of each year (mm/dd).
- # numArgs : 2,
- # toSummary : function(type, y1, y2) {
- # return 'yearly, on ' + y1 + '/' + y2;
- # },
- # calcNextDate : function(info, date) {
- # date.setFullYear(date.getFullYear() + 1);
- # date.setMonth(info.y1 - 1);
- # date.setDate(info.y2);
- # return date;
- # }
- # },
- # 'a' : { // Add-days: _14_ days after each time the Action is completed.
- # numArgs : 1,
- # toSummary : function(type, a1) {
- # return a1 + ' days after completion';
- # },
- # calcNextDate : function(info, date) {
- # date.setDate(date.getDate() + info.a1);
- # return date;
- # }
- # }
- #}
- def repeat_type()
- # repeat_hash = {
- # :none => {#Do not repeat
- # :to_summary => lambda {|| return ''},
- # :calc_next_date => lambda {|info| return nil}
- # },
- # :w => {#Next day of week, every sunday
- # :to_summary => lambda{|w1| return "every" + dayOfWeekChoices[w1][0]},#TODO:see if Time.now can be used to replace this
- # :calc_next_date => lambda {|info| return Time.now.next_week.method(info)}#DEBUG: check this
- # },
- # :m => {#Next day of month: on the 10th day of every month
- # :to_summary => lambda{|m1| return "monthly, on day " + m1},
- # :calc_next_date => lambda{|info| return Time.now.next_month.change(:day => info.m1)}#TEST needed
- # },
- # :n => {
- # },
- # :y => {
- # },
- # :a => {
- # }
- # }
- end #end repeat_type
- #Action.repeatCodeToInfo = function(code, info) {
- # info = info || {}; // An info is a hash obj.
- # code = code || ''; // A code is a string, like 'm:1:31'.
- # var parts = code.split(':');
- # info.type = parts[0] || '';
- # for (var i = 1; i < parts.length; i++)
- # info[info.type + i] = TrimPath.junctionUtil.nanToNull(parseInt(parts[i]));
- # return info;
- #}
- def repeat_code_to_info(code,info)
- end #end repeat_code_to_info
- #Action.repeatInfoToCode = function(info) { // TODO: Need validation and error messages.
- # var code = null;
- # if (info != null &&
- # info.type != null &&
- # info.type.length > 0) {
- # var repeatType = Action.repeatType[info.type];
- # if (repeatType != null) {
- # var args = [];
- # for (var i = 0; i < repeatType.numArgs; i++) {
- # var arg = TrimPath.junctionUtil.nanToNull(parseInt(info[info.type + (i + 1)]));
- # if (arg != null &&
- # arg >= 0)
- # args.push(arg);
- # }
- # if (args.length == repeatType.numArgs)
- # code = info.type + ':' + args.join(':');
- # }
- # }
- # return code;
- #}
- def repeat_info_to_code(info)
- end #end repeat_info_to_code
- #Action.prototype.repeatSummary = function() {
- # var result = "";
- # if (this.repeat != null &&
- # this.repeat.length > 0) {
- # var parts = this.repeat.split(':');
- # var repeatType = Action.repeatType[parts[0] || ''];
- # if (repeatType != null &&
- # repeatType.toSummary != null)
- # result = repeatType.toSummary.apply(null, parts);
- # }
- # return result;
- #}
- def repeat_summary()
- end #end repeat_summary
- #Action.repeatCalcNextDate = function(code, now) {
- # if (code != null) {
- # var info = Action.repeatCodeToInfo(code);
- # if (info != null) {
- # var repeatType = Action.repeatType[info.type];
- # if (repeatType != null &&
- # repeatType.calcNextDate != null)
- # return repeatType.calcNextDate(info, now || new Date());
- # }
- # }
- # return null;
- #}
- def repeat_calc_next_date(code,now)
- end #end repeat_calc_next_date
- #Action.prototype.afterUpdate = function() {
- # var nowa = new Date();
- # var nowb = new Date();
- # var nowStr = TrimPath.junctionUtil.toLocalDateString(nowa);
- # if (this.repeat != null &&
- # this.repeat.length > 0 &&
- # this.completedAt != null &&
- # TrimPath.junctionUtil.toLocalDateString(this.completedAt) == nowStr) {
- # var next = Action.newInstance();
- # next.name = this.name;
- # next.notes = this.notes;
- # next.contextId = this.contextId;
- # next.projectId = this.projectId;
- # next.color = this.color;
- # next.repeat = this.repeat;
- #
- # // Calculate a repeat period for the activeAt
- # if (this.activeAt != null && TrimPath.junctionUtil.toLocalDateString(this.activeAt) > nowStr)
- # {
- # nowa = new Date(this.activeAt);
- # }
- # next.activeAt = Action.repeatCalcNextDate(this.repeat, nowa);
- # // JGC addition: Calculate a repeat period for the dueAt as well
- # if (this.dueAt != null && TrimPath.junctionUtil.toLocalDateString(this.dueAt) > nowStr)
- # {
- # nowb = new Date(this.dueAt);
- # // TODO: Change this.repeat to the actual code that's needed
- # }
- # var dueRepeat = (this.dueAt - this.activeAt) + next.activeAt;
- # next.dueAt = Action.repeatCalcNextDate(dueRepeat, nowb);
- # // save the changes
- # if ((next.activeAt != null) || (next.dueAt != null))
- # next.save();
- # }
- #}
- def after_update()
- end #end after_update
- end
Add Comment
Please, Sign In to add comment