Class: Fae::Change
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Fae::Change
- Includes:
- ChangeConcern, Sortable
- Defined in:
- app/models/fae/change.rb
Class Method Summary collapse
- .current_user ⇒ Object
-
.current_user=(user) ⇒ Object
writing current_user to thread for thread safety.
- .filter(params) ⇒ Object
- .unique_changeable_types ⇒ Object
Class Method Details
.current_user ⇒ Object
17 18 19 |
# File 'app/models/fae/change.rb', line 17 def current_user Thread.current[:current_user] end |
.current_user=(user) ⇒ Object
writing current_user to thread for thread safety
13 14 15 |
# File 'app/models/fae/change.rb', line 13 def current_user=(user) Thread.current[:current_user] = user end |
.filter(params) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/models/fae/change.rb', line 25 def filter(params) # build conditions if specific params are present conditions = {} conditions[:user_id] = params['user'] if params['user'].present? conditions[:changeable_type] = params['model'] if params['model'].present? conditions[:change_type] = params['type'] if params['type'].present? date_scope = case params['date'] when 'Last Hour' then ['fae_changes.updated_at >= ?', 60.minutes.ago] when 'Last Day' then ['fae_changes.updated_at >= ?', 1.day.ago] when 'Last Week' then ['fae_changes.updated_at >= ?', 1.week.ago] when 'Last Month' then ['fae_changes.updated_at >= ?', 1.month.ago] end # use good 'ol MySQL to search if search param is present search = [] if params['search'].present? search = ["fae_users.first_name LIKE ? OR fae_users.last_name LIKE ? OR fae_changes.updated_attributes LIKE ? OR fae_changes.changeable_type LIKE ? OR fae_changes.change_type LIKE ?", "%#{params['search']}%", "%#{params['search']}%", "%#{params['search']}%", "%#{params['search']}%", "%#{params['search']}%"] end # apply conditions and search from above to our scope order(id: :desc) .includes(:user).references(:user) .where(Arel.sql(date_scope)).where(conditions).where(Arel.sql(search)) end |
.unique_changeable_types ⇒ Object
21 22 23 |
# File 'app/models/fae/change.rb', line 21 def unique_changeable_types pluck(:changeable_type).uniq.sort.map{ |changeable_type| [changeable_type.gsub('Fae::',''), changeable_type] } end |