event_calendar集成导致错误未定义方法`scoped'

时间:2014-11-16 07:36:02

标签: ruby-on-rails ruby

我正在使用gem event_calendar处理事件日历应用程序。但是当我加载rails服务器时。弹出以下错误。

undefined method `scoped' for #<Class:0x007f2547fb8830>

完整的框架跟踪是:

activerecord (4.1.7) lib/active_record/dynamic_matchers.rb:26:in `method_missing'
event-calendar (2.3.3) lib/event_calendar.rb:53:in `events_for_date_range'
event-calendar (2.3.3) lib/event_calendar.rb:28:in `event_strips_for_month'
actionpack (4.1.7) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.7) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.7) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.7) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.7) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.7) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.7) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.7) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.7) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.7) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.7) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.7) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.7) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.7) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.7) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.7) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.7) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.7) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.7) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.7) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.7) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.7) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.7) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.7) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.7) lib/action_dispatch/routing/route_set.rb:678:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.7) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.7)                 lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.7) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.7) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.7) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.7) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.7) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.7) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.7) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.7) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.7) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.7) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.7) lib/rails/engine.rb:514:in `call'
railties (4.1.7) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/kiran/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/kiran/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/kiran/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

2 个答案:

答案 0 :(得分:1)

这个gem与Rails 4不兼容.Github上有一个pull request open来解决这个问题,但是现在它看起来只有Rails 2和3兼容。

答案 1 :(得分:1)

我离专家不远,但您仍然可以通过在config / initializers文件夹中包含更改来覆盖gem。创建一个名为event_calendar.rb的文件,并将@vanboom提交的代码粘贴到github上:

 module EventCalendar

              def self.included(base)
                base.send :extend, ClassMethods
              end

              module ClassMethods

                def has_event_calendar(options={})
                  cattr_accessor :start_at_field, :end_at_field 
                  self.start_at_field = ( options[:start_at_field] ||= :start_at).to_s
                  self.end_at_field   = ( options[:end_at_field]   ||= :end_at  ).to_s
                  alias_attribute :start_at, start_at_field unless start_at_field == 'start_at'
                  alias_attribute :end_at,   end_at_field   unless end_at_field   == 'end_at'
                  before_save :adjust_all_day_dates
                  send :include, InstanceMethods
                end

                # For the given month, find the start and end dates
                # Find all the events within this range, and create event strips for them
                def event_strips_for_month(shown_date, first_day_of_week=0, find_options = {})
                  if first_day_of_week.is_a?(Hash)
                    find_options.merge!(first_day_of_week)
                    first_day_of_week =  0
                  end
                  strip_start, strip_end = get_start_and_end_dates(shown_date, first_day_of_week)
                  events = events_for_date_range(strip_start, strip_end, find_options)
                  event_strips = create_event_strips(strip_start, strip_end, events)
                  event_strips
                end

                # Expand start and end dates to show the previous month and next month's days,
                # that overlap with the shown months display
                def get_start_and_end_dates(shown_date, first_day_of_week=0)
                  # start with the first day of the given month
                  start_of_month = Date.civil(shown_date.year, shown_date.month, 1)
                  # the end of last month
                  strip_start = beginning_of_week(start_of_month, first_day_of_week)
                  # the beginning of next month, unless this month ended evenly on the last day of the week
                  if start_of_month.next_month == beginning_of_week(start_of_month.next_month, first_day_of_week)
                    # last day of the month is also the last day of the week
                    strip_end = start_of_month.next_month
                  else
                    # add the extra days from next month
                    strip_end = beginning_of_week(start_of_month.next_month + 7, first_day_of_week)
                  end
                  [strip_start, strip_end]
                end

                # Get the events overlapping the given start and end dates
                def events_for_date_range(start_d, end_d, find_options = {})
                  self.where(find_options).where(
                    "(? <= #{self.quoted_table_name}.#{self.end_at_field}) AND (#{self.quoted_table_name}.#{self.start_at_field}< ?)", start_d.to_time.utc, end_d.to_time.utc).order(
                    "#{self.quoted_table_name}.#{self.start_at_field} ASC"
                  )
                end

                # Create the various strips that show events.
                def create_event_strips(strip_start, strip_end, events)
                  # create an inital event strip, with a nil entry for every day of the displayed days
                  event_strips = [[nil] * (strip_end - strip_start + 1)]

                  events.each do |event|
                    cur_date = event.start_at.to_date
                    end_date = event.end_at.to_date
                    cur_date, end_date = event.clip_range(strip_start, strip_end)
                    start_range = (cur_date - strip_start).to_i
                    end_range = (end_date - strip_start).to_i

                    # make sure the event is within our viewing range
                    if (start_range <= end_range) and (end_range >= 0) 
                      range = start_range..end_range

                      open_strip = space_in_current_strips?(event_strips, range)

                      if open_strip.nil?
                        # no strips open, make a new one
                        new_strip = [nil] * (strip_end - strip_start + 1)
                        range.each {|r| new_strip[r] = event}
                        event_strips << new_strip
                      else
                        # found an open strip, add this event to it
                        range.each {|r| open_strip[r] = event}
                      end
                    end
                  end
                  event_strips
                end

                def space_in_current_strips?(event_strips, range)
                  open_strip = nil
                  for strip in event_strips
                    strip_is_open = true
                    range.each do |r|
                      # overlapping events on this strip
                      if !strip[r].nil?
                        strip_is_open = false
                        break
                      end
                    end

                    if strip_is_open
                      open_strip = strip
                      break
                    end
                  end
                  open_strip
                end

                def days_between(first, second)
                  if first > second
                    second + (7 - first)
                  else
                    second - first
                  end
                end

                def beginning_of_week(date, start = 0)
                  days_to_beg = days_between(start, date.wday)
                  date - days_to_beg
                end

              end

              # Instance Methods
              # Override in your model as needed
              module InstanceMethods
                def year
                  date.year
                end

                def month
                  date.month
                end

                def day
                  date.day
                end

                def all_day
                  self[:all_day]
                end

                def color
                  self[:color] || '#9aa4ad'
                end

                def days
                  end_at.to_date - start_at.to_date
                end

                # start_d - start of the month, or start of the week
                # end_d - end of the month, or end of the week
                def clip_range(start_d, end_d)
                  # make sure we are comparing date objects to date objects,
                  # otherwise timezones can cause problems
                  start_at_d = start_at.to_date
                  end_at_d = end_at.to_date
                  # Clip start date, make sure it also ends on or after the start range
                  if (start_at_d < start_d and end_at_d >= start_d)
                    clipped_start = start_d
                  else
                    clipped_start = start_at_d
                  end

                  # Clip end date
                  if (end_at_d > end_d)
                    clipped_end = end_d
                  else
                    clipped_end = end_at_d
                  end
                  [clipped_start, clipped_end]
                end

                def adjust_all_day_dates
                  if self.all_day
                    self.start_at = self.start_at.beginning_of_day

                    if self.end_at
                      self.end_at = self.end_at.beginning_of_day + 1.day - 1.second
                    else
                      self.end_at = self.start_at + 1.day - 1.second
                    end
                  end
                end

              end
            end

            require 'event_calendar/railtie' if defined?(::Rails::Railtie)