Class: Event
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Event
- Defined in:
- app/models/event.rb
Overview
Instance Attribute Summary collapse
-
#body ⇒ String
Optional description.
-
#city ⇒ String
Is the event’s reference city.
-
#created_at ⇒ Datetime
Date when the record was created.
-
#date_on ⇒ Date
Event’s date.
-
#gender ⇒ String
is an enum with the possible type of event.
-
#ids ⇒ Array
Is an attr_accessor, Is the list of related User, is required for create o update related meeting.
-
#max_user ⇒ Integer
is mandatory, default is 10.
-
#start_at ⇒ Datetime
Is an attr_accessor, is required, is used for make related Meeting.
-
#state ⇒ String
Is an attr_accessor, is the new state for related Meeting.
-
#updated_at ⇒ Datetime
Date when the record was updated.
-
#user ⇒ Object
Is an attr_accessor, Is a reference user.
Class Method Summary collapse
Instance Method Summary collapse
-
#audits ⇒ Array
Of related Audit.
-
#categories(user) ⇒ String
All categorycategory.title planed for this event for a User.
- #future(start_on: Time.zone.today-1.month, stop_on: Time.zone.today) ⇒ Array
-
#meetings ⇒ Array
Of related Meeting.
- #start_on ⇒ String, Null
- #status(user) ⇒ Symbol
-
#time(user) ⇒ String
Strftime of meetingmeeting.start_at from first related Meeting.
-
#update_status! ⇒ True, False
Update the status of the Meeting related to an User.
-
#users ⇒ Array
Of related User.
Methods inherited from ApplicationRecord
Instance Attribute Details
#body ⇒ String
Returns optional description.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#city ⇒ String
Returns is the event’s reference city.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#created_at ⇒ Datetime
Returns date when the record was created.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#date_on ⇒ Date
Returns event’s date.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#gender ⇒ String
is an enum with the possible type of event. Is mandatory.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#ids ⇒ Array
Is an attr_accessor, Is the list of related User, is required for create o update related meeting
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#max_user ⇒ Integer
is mandatory, default is 10
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#start_at ⇒ Datetime
Is an attr_accessor, is required, is used for make related Meeting
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#state ⇒ String
Is an attr_accessor, is the new state for related Meeting
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#updated_at ⇒ Datetime
Returns date when the record was updated.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#user ⇒ Object
Is an attr_accessor, Is a reference user
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
Class Method Details
.between(start_on: Time.zone.today-1.month, stop_on: Time.zone.today) ⇒ Array
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
Instance Method Details
#audits ⇒ Array
Returns of related Audit.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#categories(user) ⇒ String
Returns All Event.categorycategory.title planed for this event for a User.
99 100 101 |
# File 'app/models/event.rb', line 99 def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end |
#future(start_on: Time.zone.today-1.month, stop_on: Time.zone.today) ⇒ Array
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#meetings ⇒ Array
Returns of related Meeting.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |
#start_on ⇒ String, Null
82 83 84 |
# File 'app/models/event.rb', line 82 def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end |
#status(user) ⇒ Symbol
88 89 90 |
# File 'app/models/event.rb', line 88 def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end |
#time(user) ⇒ String
Returns strftime of Event.meetingmeeting.start_at from first related Meeting.
93 94 95 96 |
# File 'app/models/event.rb', line 93 def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end |
#update_status! ⇒ True, False
Update the status of the Meeting related to an User
106 107 108 109 110 111 112 113 114 |
# File 'app/models/event.rb', line 106 def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end |
#users ⇒ Array
Returns of related User.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/event.rb', line 61 class Event < ApplicationRecord has_many :meetings, dependent: :destroy has_many :audits, through: :meetings has_many :users, through: :audits has_many :user_meetings, ->(e) { where(audit_id: e.user.audit_ids) }, class_name: 'Meeting', inverse_of: :event attr_accessor :start_at, :state, :ids, :user validates :date_on, presence: true, uniqueness: { scope: %i[city gender] } validates :start_at, presence: true after_save :add_meetings enum gender: { analisys: 0, visit: 1 } default_scope -> { order('date_on asc') } scope :between, ->(start_on: Time.zone.today - 1.month, stop_on: Time.zone.today) { where('date_on >= :start_on and date_on <= :stop_on', start_on: start_on, stop_on: stop_on) } scope :future, ->(date: Time.zone.today) { where('date_on >= ?', date) } scope :confirmed, -> { joins(:meetings).where(meetings: {status: :confirmed}) } # @return [String] {start_at} to string if present # @return [Null] if {start_at} is null def start_on start_at.to_datetime.strftime('%d/%m/%Y %H:%M:%S') if start_at.present? end # @return [Symbol] {audit.status} of first {Audit} # @return [Symbol] :not_assigned if no any {Audit} is present def status(user) meetings.find_by(audit_id: user.audit_ids).try(:status) || :not_assigned end # @return [String] strftime of {meeting.start_at} from first related {Meeting} def time(user) meeting = meetings.find_by(audit_id: user.audit_ids) meeting.start_at.strftime('%H:%M') end # @return [String] All {category.title} planed for this event for a {User} def categories(user) meetings.where(audit_id: user.audit_ids).map { |m| m.category.title }.join(', ') end # Update the status of the {Meeting} related to an User # @return [True] if all {Meeting} are updated # @return [False] if {Meeting} isn't updated def update_status! new_status = case status(user) when 'proposed' then 2 when 'waiting' then 3 when 'confirmed' then 0 when 'blocked' then 1 end user_meetings.find_each { |m| m.update(status: new_status) } end private # Add or update all {Meeting} from {ids} def add_meetings audits = ids.compact audits.each { |audit| meetings.find_or_initialize_by(audit_id: audit).update(start_at: start_at, status: state) } end end |