module ProMotion::DataTable

Public Class Methods

included(base) click to toggle source
# File lib/project/pro_motion/data_table.rb, line 134
def self.included(base)
  base.extend(TableClassMethods)
end

Public Instance Methods

cell_at(args = {}) click to toggle source
# File lib/project/pro_motion/data_table.rb, line 104
def cell_at(args = {})
  index_path = args.is_a?(Hash) ? args[:index_path] : args
  c = object_at_index(index_path).cell
  set_data_cell_defaults(c)
end
controller(controller, didChangeObject: task, atIndexPath: index_path, forChangeType: change_type, newIndexPath: new_index_path) click to toggle source
# File lib/project/pro_motion/data_table.rb, line 179
def controller(controller, didChangeObject: task, atIndexPath: index_path, forChangeType: change_type, newIndexPath: new_index_path)
  unless searching?
    case change_type
    when NSFetchedResultsChangeInsert
      table_view.insertRowsAtIndexPaths([new_index_path], withRowAnimation: UITableViewRowAnimationAutomatic)
    when NSFetchedResultsChangeDelete
      table_view.deleteRowsAtIndexPaths([index_path], withRowAnimation: UITableViewRowAnimationAutomatic)
    when NSFetchedResultsChangeUpdate
      table_view.reloadRowsAtIndexPaths([index_path], withRowAnimation: UITableViewRowAnimationAutomatic)
    end
  end
end
controllerDidChangeContent(controller) click to toggle source
# File lib/project/pro_motion/data_table.rb, line 192
def controllerDidChangeContent(controller)
  table_view.endUpdates unless searching?
end
controllerWillChangeContent(controller) click to toggle source
# File lib/project/pro_motion/data_table.rb, line 171
def controllerWillChangeContent(controller)
  # TODO - we should update the search results table when a new record is added
  # or deleted or changed. For now, when the data changes, the search doesn't
  # update. Closing the search will update the data and then searching again
  # will show the new or changed content.
  table_view.beginUpdates unless searching?
end
data_model() click to toggle source
# File lib/project/pro_motion/data_table.rb, line 114
def data_model
  self.class.data_model
end
data_scope() click to toggle source
# File lib/project/pro_motion/data_table.rb, line 118
def data_scope
  self.class.data_scope
end
fetch_controller() click to toggle source
# File lib/project/pro_motion/data_table.rb, line 86
def fetch_controller
  if searching?
    search_fetch_controller
  else
    @fetch_controller ||= NSFetchedResultsController.alloc.initWithFetchRequest(
      fetch_scope.fetch_request,
      managedObjectContext: fetch_scope.context,
      sectionNameKeyPath: nil,
      cacheName: nil
    )
  end
end
fetch_scope() click to toggle source
# File lib/project/pro_motion/data_table.rb, line 54
def fetch_scope
  @_fetch_scope ||= begin
    if respond_to?(:model_query)
      data_with_scope = model_query
    else
      data_with_scope = data_model.send(data_scope)
    end

    if data_with_scope.sort_descriptors.blank?
      # Try to be smart about how we sort things if a sort descriptor doesn't exist
      attributes = data_model.send(:attribute_names)
      sort_attribute = nil
      [:updated_at, :created_at, :id].each do |att|
        sort_attribute ||= att if attributes.include?(att.to_s)
      end

      if sort_attribute

        unless data_scope == :all
          mp "The `#{data_model}` model scope `#{data_scope}` needs a sort descriptor. Add sort_by(:property) to your scope. Currently sorting by :#{sort_attribute}.", force_color: :yellow
        end
        data_model.send(data_scope).sort_by(sort_attribute)
      else
        # This is where the application says goodbye and dies in a fiery crash.
        mp "The `#{data_model}` model scope `#{data_scope}` needs a sort descriptor. Add sort_by(:property) to your scope.", force_color: :yellow
      end
    else
      data_with_scope
    end
  end
end
numberOfSectionsInTableView(_) click to toggle source

UITableViewDelegate methods

# File lib/project/pro_motion/data_table.rb, line 139
def numberOfSectionsInTableView(_)
  fetch_controller.sections.count
end
object_at_index(i) click to toggle source
# File lib/project/pro_motion/data_table.rb, line 110
def object_at_index(i)
  fetch_controller.objectAtIndexPath(i)
end
on_cell_created(cell, data) click to toggle source
# File lib/project/pro_motion/data_table.rb, line 99
def on_cell_created(cell, data)
  # Do not call super here
  self.rmq.build(cell)
end
original_search_string() click to toggle source
# File lib/project/pro_motion/data_table.rb, line 130
def original_search_string
  search_string
end
screen_setup() click to toggle source
# File lib/project/pro_motion/data_table.rb, line 25
def screen_setup
  set_up_fetch_controller

  set_up_header_footer_views
  set_up_searchable
  set_up_refreshable
  set_up_longpressable
  set_up_row_height
end
search_string() click to toggle source
# File lib/project/pro_motion/data_table.rb, line 126
def search_string
  @_data_table_search_string
end
searching?() click to toggle source
# File lib/project/pro_motion/data_table.rb, line 122
def searching?
  @_data_table_searching || false
end
set_up_fetch_controller() click to toggle source
# File lib/project/pro_motion/data_table.rb, line 35
def set_up_fetch_controller
  error_ptr = Pointer.new(:object)
  fetch_controller.delegate = self

  unless fetch_controller.performFetch(error_ptr)
    raise "Error performing fetch: #{error_ptr[2].description}"
  end
end
tableView(table_view, numberOfRowsInSection: section) click to toggle source
# File lib/project/pro_motion/data_table.rb, line 143
def tableView(table_view, numberOfRowsInSection: section)
  fetch_controller.sections[section].numberOfObjects
end
table_data() click to toggle source

This has to be defined in order to inherit everything from TableScreen

# File lib/project/pro_motion/data_table.rb, line 21
def table_data
  [{cells:[]}]
end
table_view() click to toggle source
# File lib/project/pro_motion/data_table.rb, line 16
def table_view
  self.view
end
update_table_data(notification = nil) click to toggle source
# File lib/project/pro_motion/data_table.rb, line 44
def update_table_data(notification = nil)
  if notification.nil?
    table_view.reloadData
  else
    Dispatch::Queue.main.async do
      fetch_controller.managedObjectContext.mergeChangesFromContextDidSaveNotification(notification)
    end
  end
end