Class: BigBrotha::Config
- Inherits:
-
Object
- Object
- BigBrotha::Config
- Defined in:
- lib/bigbrotha/config.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#add(model, creator_column, columns = [], timing, event) ⇒ Object
The argument :timing indicates whether the callback is to be run :before, :after the event(:save, :create, :update).
Class Method Details
.get_value(obj, column) ⇒ Object
33 34 35 36 37 |
# File 'lib/bigbrotha/config.rb', line 33 def self.get_value(obj, column) column = column.to_sym return obj if column == :self obj.respond_to?(column) ? obj.send(column) : nil end |
Instance Method Details
#add(model, creator_column, columns = [], timing, event) ⇒ Object
The argument :timing indicates whether the callback is to be run :before, :after the event(:save, :create, :update).
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bigbrotha/config.rb', line 6 def add(model, creator_column, columns = [], timing, event) raise NotActiveRecordModel.new unless model < ActiveRecord::Base columns = Array(columns) event = event.to_sym timing = timing.to_sym model.set_callback event, timing, ->(obj) { changed_columns = event == :create ? columns : columns.map { |c| c.to_s } & obj.changed censored_texts = {} changed_columns.each do |column| content_column = model.to_s + "." + column.to_s creator = Config.get_value(obj, creator_column) column_value = Config.get_value(obj, column) censored_text = Censor.censor_text!(creator, column_value, content_column) censored_texts[column] = censored_text end if timing == :after obj.update_columns(censored_texts) elsif timing == :before censored_texts.each {|k,v| obj.send(k.to_s+"=", v)} end } end |