{"action":"created","comment":{"url":"https://api.github.com/repos/fsek/web/pulls/comments/27075437","id":27075437,"diff_hunk":"@@ -0,0 +1,248 @@\n+# encoding: UTF-8\n+class CafeWork < ActiveRecord::Base\n+  # Associations\n+  belongs_to :profile\n+  has_and_belongs_to_many :councils\n+\n+  # Validations\n+  validates :work_day, :pass, :lp, :lv, presence: true\n+  validates :pass, :lp, inclusion: { in: 1..4 }\n+  validates :lv, inclusion: { in: 1..7 }\n+  validates :name, :lastname, :phone, :email, presence: true, if: :has_worker?\n+  validates :pass, uniqueness: { scope: [:work_day, :lv, :lp, :d_year] }\n+\n+  # Scopes\n+  scope :with_worker, -> { where('profile_id IS NOT null OR access_code IS NOT null') }\n+  scope :between, ->(from, to) { where(work_day: from..to) }\n+  scope :ascending, -> { order(pass: :asc) }\n+  scope :week, ->(week) { where(lv: week) }\n+  scope :period, ->(p) { where(lp: p) }\n+  scope :year, ->(y) { where(d_year: y) }\n+\n+  after_update :send_email, if: :has_worker?\n+\n+  # A custom class for the worker\n+  # For more information see Assignee model or\n+  # read comments above methods\n+  def worker\n+    @worker || Assignee.new(worker_attributes)\n+  end\n+\n+  # Sends email to worker\n+  # /d.wessman\n+  def send_email\n+    CafeMailer.sign_up_email(self).deliver_now\n+  rescue\n+    Rails.logger.info 'Mailer could not connect, rescued here'\n+  end\n+\n+  # Prepares work for a user to sign up, without saving\n+  # Will only change attributes if there is no worker and\n+  # user is present.\n+  # /d.wessman\n+  def load(user)\n+    self.attributes = worker.load_profile(user)\n+  end\n+\n+  # Shows different status texts depending on the user.\n+  # /d.wessman\n+  def status_text(user)\n+    case status_view(user)\n+      when 0\n+        return 'Fyll i uppgifter och tryck på Spara för att skriva upp dig och arbeta på passet.'\n+      when 1\n+        return 'Du är uppskriven för att arbeta på passet.'\n+      when 2\n+        return 'Passet är redan bokat.'\n+      when 3\n+        return 'Passet är bokat, fyll i koden som gavs vid anmälan för att redigera.'\n+    end\n+  end\n+\n+  # Gives different statuses for the view\n+  # 0 = everyone can sign up\n+  # 1 = can be edited, either logged in or authorized\n+  # 2 = shows no form\n+  # 3 = shows form for authorization\n+  # /d.wessman\n+  def status_view(user)\n+    if access_code.present?\n+      return 3\n+    elsif has_worker?\n+      return (owner?(user) ? 1 : 2)\n+    end\n+    0\n+  end\n+\n+  def add_or_update(worker_params, user)\n+    if has_worker?\n+      update_worker(worker_params, user)\n+    else\n+      add_worker(worker_params, user)\n+    end\n+  end\n+\n+  # User to update worker, checks for edit-access and triggers at_update\n+  # /d.wessman\n+  def add_worker(worker_params, user)\n+    if has_worker?\n+      errors.add('Arbetare', 'passet har redan en.')\n+      return false\n+    end\n+\n+    # Should be done with a bang when the error handling works\n+    # Ref: https://github.com/fsek/web/issues/93\n+    # /d.wessman\n+    self.attributes = worker_params\n+    self.attributes = Assignee.setup(worker_attributes, user).attributes\n+    save\n+  end\n+\n+  # User to update worker, checks for edit-access\n+  # /d.wessman\n+  def update_worker(worker_params, user)\n+    if !owner?(user) && !authorize(worker_params[:access_code])\n+      errors.add('Auktorisering',\n+                 'misslyckades, du har inte rättighet att redigera eller skrev fel kod.')\n+      return false\n+    end\n+\n+    # Should be done with a bang when the error handling works\n+    # Ref: https://github.com/fsek/web/issues/93\n+    # /d.wessman\n+    self.attributes = worker_params\n+    self.attributes = Assignee.setup(worker_attributes, user).attributes\n+    save\n+  end\n+\n+  # Remove-function used by the worker\n+  # /d.wessman\n+  def remove_worker(user, access)\n+    if !owner?(user) && !authorize(access)\n+      errors.add('Auktorisering',\n+                 'misslyckades, du har inte rättighet att ta bort eller skrev fel kod.')\n+      return false\n+    end\n+\n+    clear_worker\n+  end\n+\n+  # Method to remove the worker from current work.\n+  # /d.wessman\n+  def clear_worker\n+    self.attributes = worker.clear_attributes\n+    self.utskottskamp = false\n+    councils.clear\n+    self.save!(validate: false)\n+  end\n+\n+  # Returns true if the profiles are similar and not nil\n+  # /d.wessman\n+  def owner?(user)\n+    user.present? && user.profile.present? && profile.present? && user.profile == profile\n+  end\n+\n+  # Returns true if the user can edit the object\n+  # /d.wessman\n+  def edit?(user)\n+    editable? && (!has_worker? || owner?(user))\n+  end\n+\n+  def editable?\n+    work_day > Time.zone.now\n+  end\n+\n+  # Returns true only if the access_code is correct\n+  # /d.wessman\n+  def authorize(access)\n+    access.present? && access_code.present? && access_code == access\n+  end\n+\n+  # Returns true if there is a worker\n+  # /d.wessman\n+  def has_worker?\n+    worker.present?\n+  end\n+\n+  # Used to print date in a usable format.\n+  # /d.wessman\n+  def print_time\n+    %(#{start.strftime('%H:%M')}-#{stop.strftime('%H:%M')})\n+  end\n+\n+  # Used to print out date, reading week and work number\n+  # /d.wessman\n+  def print\n+    %(#{print_date} LV: #{lv} Pass: #{pass})\n+  end\n+\n+  def print_date\n+    %(#{print_time}, #{start.strftime('%A %d/%m')})\n+  end\n+\n+  # Prints the url or path for the current object\n+  def p_url\n+    Rails.application.routes.url_helpers.cafe_work_url(id, host: PUBLIC_URL)\n+  end\n+\n+  def p_path\n+    Rails.application.routes.url_helpers.cafe_work_path(id)\n+  end\n+\n+  def as_json(*)\n+    {\n+        id: id,\n+        title: %(Cafepass #{pass}),\n+        start: start.iso8601,\n+        end: stop.iso8601,\n+        status: print,\n+        url: p_path,\n+        color: 'black',\n+        backgroundColor: b_color,\n+        textColor: 'black'\n+    }\n+  end\n+\n+  # To print start time\n+  def t_start\n+    start.strftime('%H:%M')\n+  end\n+\n+  # To print end time\n+  def t_end\n+    stop.strftime('%H:%M')\n+  end\n+\n+  def start\n+    work_day\n+  end\n+\n+  # End would be a better name, doesn't fit into code.\n+  def stop\n+    work_day + duration.hours\n+  end\n+\n+  def self.get_lv\n+    check = CafeWork.between(Time.zone.now.beginning_of_day - 2.days, Time.zone.now.end_of_day).last\n+    (check.present?) ? check.lv.to_s : '?'\n+  end\n+\n+  protected\n+\n+  def worker_attributes\n+    {\n+        name: name, lastname: lastname, email: email,","path":"app/models/cafe_work.rb","position":234,"original_position":234,"commit_id":"e6992782683b3f35349b7db3b05df1e94a573c2a","original_commit_id":"e6992782683b3f35349b7db3b05df1e94a573c2a","user":{"login":"houndci","id":6697940,"avatar_url":"https://avatars.githubusercontent.com/u/6697940?v=3","gravatar_id":"","url":"https://api.github.com/users/houndci","html_url":"https://github.com/houndci","followers_url":"https://api.github.com/users/houndci/followers","following_url":"https://api.github.com/users/houndci/following{/other_user}","gists_url":"https://api.github.com/users/houndci/gists{/gist_id}","starred_url":"https://api.github.com/users/houndci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/houndci/subscriptions","organizations_url":"https://api.github.com/users/houndci/orgs","repos_url":"https://api.github.com/users/houndci/repos","events_url":"https://api.github.com/users/houndci/events{/privacy}","received_events_url":"https://api.github.com/users/houndci/received_events","type":"User","site_admin":false},"body":"Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.","created_at":"2015-03-24T21:31:16Z","updated_at":"2015-03-24T21:31:16Z","html_url":"https://github.com/fsek/web/pull/88#discussion_r27075437","pull_request_url":"https://api.github.com/repos/fsek/web/pulls/88","_links":{"self":{"href":"https://api.github.com/repos/fsek/web/pulls/comments/27075437"},"html":{"href":"https://github.com/fsek/web/pull/88#discussion_r27075437"},"pull_request":{"href":"https://api.github.com/repos/fsek/web/pulls/88"}}},"pull_request":{"url":"https://api.github.com/repos/fsek/web/pulls/88","id":31668379,"html_url":"https://github.com/fsek/web/pull/88","diff_url":"https://github.com/fsek/web/pull/88.diff","patch_url":"https://github.com/fsek/web/pull/88.patch","issue_url":"https://api.github.com/repos/fsek/web/issues/88","number":88,"state":"open","locked":false,"title":"Merge Cafebokning into master","user":{"login":"davidwessman","id":6763624,"avatar_url":"https://avatars.githubusercontent.com/u/6763624?v=3","gravatar_id":"","url":"https://api.github.com/users/davidwessman","html_url":"https://github.com/davidwessman","followers_url":"https://api.github.com/users/davidwessman/followers","following_url":"https://api.github.com/users/davidwessman/following{/other_user}","gists_url":"https://api.github.com/users/davidwessman/gists{/gist_id}","starred_url":"https://api.github.com/users/davidwessman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidwessman/subscriptions","organizations_url":"https://api.github.com/users/davidwessman/orgs","repos_url":"https://api.github.com/users/davidwessman/repos","events_url":"https://api.github.com/users/davidwessman/events{/privacy}","received_events_url":"https://api.github.com/users/davidwessman/received_events","type":"User","site_admin":false},"body":"Preparing to launch Cafebokning","created_at":"2015-03-21T08:40:10Z","updated_at":"2015-03-24T21:31:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a00ac7515d27ad9f9bb4634727181b4b1779fe9c","assignee":null,"milestone":{"url":"https://api.github.com/repos/fsek/web/milestones/2","html_url":"https://github.com/fsek/web/milestones/Cafebokning","labels_url":"https://api.github.com/repos/fsek/web/milestones/2/labels","id":990410,"number":2,"title":"Cafebokning","description":null,"creator":{"login":"davidwessman","id":6763624,"avatar_url":"https://avatars.githubusercontent.com/u/6763624?v=3","gravatar_id":"","url":"https://api.github.com/users/davidwessman","html_url":"https://github.com/davidwessman","followers_url":"https://api.github.com/users/davidwessman/followers","following_url":"https://api.github.com/users/davidwessman/following{/other_user}","gists_url":"https://api.github.com/users/davidwessman/gists{/gist_id}","starred_url":"https://api.github.com/users/davidwessman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidwessman/subscriptions","organizations_url":"https://api.github.com/users/davidwessman/orgs","repos_url":"https://api.github.com/users/davidwessman/repos","events_url":"https://api.github.com/users/davidwessman/events{/privacy}","received_events_url":"https://api.github.com/users/davidwessman/received_events","type":"User","site_admin":false},"open_issues":2,"closed_issues":2,"state":"open","created_at":"2015-02-22T22:47:26Z","updated_at":"2015-03-21T08:42:13Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/fsek/web/pulls/88/commits","review_comments_url":"https://api.github.com/repos/fsek/web/pulls/88/comments","review_comment_url":"https://api.github.com/repos/fsek/web/pulls/comments{/number}","comments_url":"https://api.github.com/repos/fsek/web/issues/88/comments","statuses_url":"https://api.github.com/repos/fsek/web/statuses/e6992782683b3f35349b7db3b05df1e94a573c2a","head":{"label":"fsek:cafebokning","ref":"cafebokning","sha":"e6992782683b3f35349b7db3b05df1e94a573c2a","user":{"login":"fsek","id":10084654,"avatar_url":"https://avatars.githubusercontent.com/u/10084654?v=3","gravatar_id":"","url":"https://api.github.com/users/fsek","html_url":"https://github.com/fsek","followers_url":"https://api.github.com/users/fsek/followers","following_url":"https://api.github.com/users/fsek/following{/other_user}","gists_url":"https://api.github.com/users/fsek/gists{/gist_id}","starred_url":"https://api.github.com/users/fsek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fsek/subscriptions","organizations_url":"https://api.github.com/users/fsek/orgs","repos_url":"https://api.github.com/users/fsek/repos","events_url":"https://api.github.com/users/fsek/events{/privacy}","received_events_url":"https://api.github.com/users/fsek/received_events","type":"Organization","site_admin":false},"repo":{"id":27583674,"name":"web","full_name":"fsek/web","owner":{"login":"fsek","id":10084654,"avatar_url":"https://avatars.githubusercontent.com/u/10084654?v=3","gravatar_id":"","url":"https://api.github.com/users/fsek","html_url":"https://github.com/fsek","followers_url":"https://api.github.com/users/fsek/followers","following_url":"https://api.github.com/users/fsek/following{/other_user}","gists_url":"https://api.github.com/users/fsek/gists{/gist_id}","starred_url":"https://api.github.com/users/fsek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fsek/subscriptions","organizations_url":"https://api.github.com/users/fsek/orgs","repos_url":"https://api.github.com/users/fsek/repos","events_url":"https://api.github.com/users/fsek/events{/privacy}","received_events_url":"https://api.github.com/users/fsek/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/fsek/web","description":"F-sektionens hemsida","fork":false,"url":"https://api.github.com/repos/fsek/web","forks_url":"https://api.github.com/repos/fsek/web/forks","keys_url":"https://api.github.com/repos/fsek/web/keys{/key_id}","collaborators_url":"https://api.github.com/repos/fsek/web/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/fsek/web/teams","hooks_url":"https://api.github.com/repos/fsek/web/hooks","issue_events_url":"https://api.github.com/repos/fsek/web/issues/events{/number}","events_url":"https://api.github.com/repos/fsek/web/events","assignees_url":"https://api.github.com/repos/fsek/web/assignees{/user}","branches_url":"https://api.github.com/repos/fsek/web/branches{/branch}","tags_url":"https://api.github.com/repos/fsek/web/tags","blobs_url":"https://api.github.com/repos/fsek/web/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/fsek/web/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/fsek/web/git/refs{/sha}","trees_url":"https://api.github.com/repos/fsek/web/git/trees{/sha}","statuses_url":"https://api.github.com/repos/fsek/web/statuses/{sha}","languages_url":"https://api.github.com/repos/fsek/web/languages","stargazers_url":"https://api.github.com/repos/fsek/web/stargazers","contributors_url":"https://api.github.com/repos/fsek/web/contributors","subscribers_url":"https://api.github.com/repos/fsek/web/subscribers","subscription_url":"https://api.github.com/repos/fsek/web/subscription","commits_url":"https://api.github.com/repos/fsek/web/commits{/sha}","git_commits_url":"https://api.github.com/repos/fsek/web/git/commits{/sha}","comments_url":"https://api.github.com/repos/fsek/web/comments{/number}","issue_comment_url":"https://api.github.com/repos/fsek/web/issues/comments{/number}","contents_url":"https://api.github.com/repos/fsek/web/contents/{+path}","compare_url":"https://api.github.com/repos/fsek/web/compare/{base}...{head}","merges_url":"https://api.github.com/repos/fsek/web/merges","archive_url":"https://api.github.com/repos/fsek/web/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/fsek/web/downloads","issues_url":"https://api.github.com/repos/fsek/web/issues{/number}","pulls_url":"https://api.github.com/repos/fsek/web/pulls{/number}","milestones_url":"https://api.github.com/repos/fsek/web/milestones{/number}","notifications_url":"https://api.github.com/repos/fsek/web/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/fsek/web/labels{/name}","releases_url":"https://api.github.com/repos/fsek/web/releases{/id}","created_at":"2014-12-05T09:13:19Z","updated_at":"2015-03-24T14:38:54Z","pushed_at":"2015-03-24T21:30:49Z","git_url":"git://github.com/fsek/web.git","ssh_url":"git@github.com:fsek/web.git","clone_url":"https://github.com/fsek/web.git","svn_url":"https://github.com/fsek/web","homepage":"http://fsektionen.se","size":209884,"stargazers_count":1,"watchers_count":1,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":46,"forks":1,"open_issues":46,"watchers":1,"default_branch":"master"}},"base":{"label":"fsek:master","ref":"master","sha":"1abdf3ab43f6a0c7a0461a202636a7b2686ab1cc","user":{"login":"fsek","id":10084654,"avatar_url":"https://avatars.githubusercontent.com/u/10084654?v=3","gravatar_id":"","url":"https://api.github.com/users/fsek","html_url":"https://github.com/fsek","followers_url":"https://api.github.com/users/fsek/followers","following_url":"https://api.github.com/users/fsek/following{/other_user}","gists_url":"https://api.github.com/users/fsek/gists{/gist_id}","starred_url":"https://api.github.com/users/fsek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fsek/subscriptions","organizations_url":"https://api.github.com/users/fsek/orgs","repos_url":"https://api.github.com/users/fsek/repos","events_url":"https://api.github.com/users/fsek/events{/privacy}","received_events_url":"https://api.github.com/users/fsek/received_events","type":"Organization","site_admin":false},"repo":{"id":27583674,"name":"web","full_name":"fsek/web","owner":{"login":"fsek","id":10084654,"avatar_url":"https://avatars.githubusercontent.com/u/10084654?v=3","gravatar_id":"","url":"https://api.github.com/users/fsek","html_url":"https://github.com/fsek","followers_url":"https://api.github.com/users/fsek/followers","following_url":"https://api.github.com/users/fsek/following{/other_user}","gists_url":"https://api.github.com/users/fsek/gists{/gist_id}","starred_url":"https://api.github.com/users/fsek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fsek/subscriptions","organizations_url":"https://api.github.com/users/fsek/orgs","repos_url":"https://api.github.com/users/fsek/repos","events_url":"https://api.github.com/users/fsek/events{/privacy}","received_events_url":"https://api.github.com/users/fsek/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/fsek/web","description":"F-sektionens hemsida","fork":false,"url":"https://api.github.com/repos/fsek/web","forks_url":"https://api.github.com/repos/fsek/web/forks","keys_url":"https://api.github.com/repos/fsek/web/keys{/key_id}","collaborators_url":"https://api.github.com/repos/fsek/web/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/fsek/web/teams","hooks_url":"https://api.github.com/repos/fsek/web/hooks","issue_events_url":"https://api.github.com/repos/fsek/web/issues/events{/number}","events_url":"https://api.github.com/repos/fsek/web/events","assignees_url":"https://api.github.com/repos/fsek/web/assignees{/user}","branches_url":"https://api.github.com/repos/fsek/web/branches{/branch}","tags_url":"https://api.github.com/repos/fsek/web/tags","blobs_url":"https://api.github.com/repos/fsek/web/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/fsek/web/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/fsek/web/git/refs{/sha}","trees_url":"https://api.github.com/repos/fsek/web/git/trees{/sha}","statuses_url":"https://api.github.com/repos/fsek/web/statuses/{sha}","languages_url":"https://api.github.com/repos/fsek/web/languages","stargazers_url":"https://api.github.com/repos/fsek/web/stargazers","contributors_url":"https://api.github.com/repos/fsek/web/contributors","subscribers_url":"https://api.github.com/repos/fsek/web/subscribers","subscription_url":"https://api.github.com/repos/fsek/web/subscription","commits_url":"https://api.github.com/repos/fsek/web/commits{/sha}","git_commits_url":"https://api.github.com/repos/fsek/web/git/commits{/sha}","comments_url":"https://api.github.com/repos/fsek/web/comments{/number}","issue_comment_url":"https://api.github.com/repos/fsek/web/issues/comments{/number}","contents_url":"https://api.github.com/repos/fsek/web/contents/{+path}","compare_url":"https://api.github.com/repos/fsek/web/compare/{base}...{head}","merges_url":"https://api.github.com/repos/fsek/web/merges","archive_url":"https://api.github.com/repos/fsek/web/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/fsek/web/downloads","issues_url":"https://api.github.com/repos/fsek/web/issues{/number}","pulls_url":"https://api.github.com/repos/fsek/web/pulls{/number}","milestones_url":"https://api.github.com/repos/fsek/web/milestones{/number}","notifications_url":"https://api.github.com/repos/fsek/web/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/fsek/web/labels{/name}","releases_url":"https://api.github.com/repos/fsek/web/releases{/id}","created_at":"2014-12-05T09:13:19Z","updated_at":"2015-03-24T14:38:54Z","pushed_at":"2015-03-24T21:30:49Z","git_url":"git://github.com/fsek/web.git","ssh_url":"git@github.com:fsek/web.git","clone_url":"https://github.com/fsek/web.git","svn_url":"https://github.com/fsek/web","homepage":"http://fsektionen.se","size":209884,"stargazers_count":1,"watchers_count":1,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":46,"forks":1,"open_issues":46,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/fsek/web/pulls/88"},"html":{"href":"https://github.com/fsek/web/pull/88"},"issue":{"href":"https://api.github.com/repos/fsek/web/issues/88"},"comments":{"href":"https://api.github.com/repos/fsek/web/issues/88/comments"},"review_comments":{"href":"https://api.github.com/repos/fsek/web/pulls/88/comments"},"review_comment":{"href":"https://api.github.com/repos/fsek/web/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/fsek/web/pulls/88/commits"},"statuses":{"href":"https://api.github.com/repos/fsek/web/statuses/e6992782683b3f35349b7db3b05df1e94a573c2a"}}}}