Class: CloudObject::Activity

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/lesli/cloud_object/activity.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cloud_object_modelClass

Returns The class of the association ‘belongs_to’.

Examples:

puts DeutscheLeibrenten::Project::Activity.cloud_object_model.new # This will display an instance of DeutscheLeibrenten::Project

Returns:

  • (Class)

    The class of the association ‘belongs_to’



67
68
69
# File 'app/models/lesli/cloud_object/activity.rb', line 67

def self.cloud_object_model
    self.reflect_on_association(:cloud_object).klass
end

.index(current_user, cloud_id, query) ⇒ Array

Returns Array of activities. Each activities contains a responses element, which is an array that has all its responses ordered by date.

Examples:

@activities = activity_model.index(
    current_user,
    profile_id,
    @query
)

Parameters:

  • current_user (User)

    The current logged user

  • cloud_id (Integer)

    Id of the cloud_object to which this activities belongs to

  • query (Query)

    that contains the search and pagination information

Returns:

  • (Array)

    Array of activities. Each activities contains a responses element, which is an array that has all its responses ordered by date



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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/lesli/cloud_object/activity.rb', line 89

def self.index(current_user, cloud_id, query)
    cloud_object_model = self.cloud_object_model
     = cloud_object_model.reflect_on_association(:account).klass
    translations_module = self.name.split("::")[0].gsub("Cloud", "").underscore

    # get search string from query params
    search_string = query[:search].downcase.gsub(" ","%") unless query[:search].blank?
    
    activities = self
    .where("#{cloud_object_model.table_name}_id".to_sym => cloud_id)
    .order(id: :desc)
    .joins("inner join users u on #{self.table_name}.users_id = u.id")
    .joins("inner join user_details ud on ud.users_id = u.id")


    # Filter results by search string
    unless search_string.blank?
        # (LOWER(ud.last_name) SIMILAR TO '%#{search_string}%') OR 
        activities = activities.where("
        (LOWER(#{self.table_name}.field_name) SIMILAR TO '%#{search_string}%') OR
        (LOWER(#{self.table_name}.value_from) SIMILAR TO '%#{search_string}%') OR
        (LOWER(#{self.table_name}.value_to) SIMILAR TO '%#{search_string}%') OR
        (LOWER(ud.first_name) SIMILAR TO '%#{search_string}%')
        ")
    end

    activities = activities.map do |activity|
        # We translate the category, first, we search in the core
        category = I18n.t("core.shared.activities_enum_category_#{activity[:category]}", default: nil)
        
        # Then we search in the engine
        category = I18n.t("#{translations_module}.shared.activities_enum_category_#{activity[:category]}", default: nil) unless category
        
        #Then we default to the real field
        category = activity[:category] unless category

        activity_attributes = activity.attributes
        activity_attributes["category"] = category
        activity_attributes["created_at_raw"] = activity[:created_at]
        activity_attributes["created_at"] = LC::Date.to_string_datetime(activity[:created_at])
        activity_attributes["updated_at"] = LC::Date.to_string_datetime(activity[:updated_at])

        user = activity.user_creator
        activity_attributes[:user_name] = user.full_name

        activity_attributes
    end

    Kaminari.paginate_array(activities).page(query[:pagination][:page]).per(query[:pagination][:perPage])
end

.log_create(current_user, cloud_object) ⇒ void

This method returns an undefined value.

Example

params = {...}
ticket = CloudHelp::Ticket.create(params)
CloudHelp::Ticket.log_create(User.first, ticket)

Parameters:

  • current_user (::User)

    The user that created the cloud_object

  • cloud_object (CloudModule::Model)

    The cloud_object that was created



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/models/lesli/cloud_object/activity.rb', line 148

def self.log_create(current_user, cloud_object)
    cloud_object.activities.create(
        user_creator: current_user,
        category: "action_create"
    )

    # If cloud_object has workflow
    if defined?(cloud_object.status)
        module_name = cloud_object.class.lesli_classname().split("::")[0].underscore
        cloud_object.activities.create(
            user_creator: current_user,
            category: "action_status",
            description: cloud_object.status&.name,
            field_name: "#{module_name}_workflow_statuses_id",
            value_to: cloud_object.status&.name
        )
    end

    yield if block_given?
end

.log_destroy(current_user, cloud_object) ⇒ void

This method returns an undefined value.

Example

ticket = CloudHelp::Ticket.find(1)
CloudHelp::TicketLogger.log_destroy(User.first, ticket)
ticket.destroy

Parameters:

  • current_user (::User)

    The user that deleted the cloud_object

  • cloud_object (CloudModule::Model)

    The cloud_object that was destroyed



228
229
230
231
232
233
234
235
# File 'app/models/lesli/cloud_object/activity.rb', line 228

def self.log_destroy(current_user, cloud_object)
    cloud_object.activities.create(
        user_creator: current_user,
        category: "action_destroy"
    )

    yield if block_given?
end

.log_show(current_user, cloud_object) ⇒ void

This method returns an undefined value.

Example

params = {...}
ticket = CloudHelp::Ticket.create(params)
CloudHelp::Ticket.log_show(User.first, ticket)

Parameters:

  • current_user (::User)

    The user that created the cloud_object

  • cloud_object (CloudModule::Model)

    The cloud_object that was shown



177
178
179
180
181
182
183
184
# File 'app/models/lesli/cloud_object/activity.rb', line 177

def self.log_show(current_user, cloud_object)
    cloud_object.activities.create(
        user_creator: current_user,
        category: "action_show"
    )

    yield if block_given?
end

.log_update(current_user, cloud_object, old_attributes, new_attributes, category: "action_update") ⇒ void

This method returns an undefined value.

Example

ticket = CloudHelp::Ticket.find(1)
old_attributes  = ticket.attributes
ticket.update(user_main: User.find(33))
new_attributes = ticket.attributes
CloudHelp::TicketLogger.log_update(User.find(1), ticket, old_attributes, new_attributes)

Parameters:

  • current_user (::User)

    The user that edited the cloud_object

  • cloud_object (CloudModule::Model)

    The cloud_object that was edited

  • old_attributes (Hash)

    The data of the record before update

  • new_attributes (Hash)

    The data of the record after update



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/models/lesli/cloud_object/activity.rb', line 198

def self.log_update(current_user, cloud_object, old_attributes, new_attributes, category: "action_update")

    # We remove values that are not tracked in the activities
    old_attributes.except!("id", "created_at", "updated_at", "deleted_at")
    old_attributes.each do |key, value|
        next if value == new_attributes[key]
        if key.include?("id")
            if key == "user_main_id" || key == "users_id" || key == "user_branch_office_id"
                update_user_field(cloud_object, current_user, key, old_attributes[key], new_attributes[key], category)
            elsif key.include?("workflow_statuses_id")
                update_workflow_status_field(cloud_object, current_user, key, old_attributes[key], new_attributes[key])
            else
                update_field(cloud_object, current_user, key, old_attributes[key], new_attributes[key], category)
            end
        else
            update_field(cloud_object, current_user, key, old_attributes[key], new_attributes[key], category)
        end
    end

    yield if block_given?
end

Instance Method Details

#user_mainUser

Returns This method will always return nil.

Returns:

  • (User)

    This method will always return nil



58
59
60
# File 'app/models/lesli/cloud_object/activity.rb', line 58

def user_main
    return nil
end