Skip to content

Commit

Permalink
share methods
Browse files Browse the repository at this point in the history
  • Loading branch information
qinmingyuan committed Mar 7, 2024
1 parent 7114b00 commit 24a2394
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
23 changes: 23 additions & 0 deletions app/models/growth/inner/text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Growth
module Inner::Text
extend ActiveSupport::Concern

included do
attribute :text_year, :string
attribute :text_month, :string
attribute :text_week, :string
attribute :text_date, :date

belongs_to :user, class_name: 'Auth::User', optional: true
belongs_to :aim, optional: true
end

def filter_hash
{
aim_id: aim_id,
text_year: text_year
}
end

end
end
19 changes: 4 additions & 15 deletions app/models/growth/model/aim_entity.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
module Growth
module Model::AimEntity
extend ActiveSupport::Concern
include Inner::Text

included do
attribute :present_point, :integer
attribute :state, :string
attribute :text_year, :string
attribute :text_month, :string
attribute :text_week, :string
attribute :text_date, :date

attribute :last_access_at, :datetime
attribute :ip, :string
attribute :reward_amount, :decimal, precision: 10, scale: 2
attribute :aim_logs_count, :integer, default: 0

belongs_to :user, class_name: 'Auth::User', optional: true

belongs_to :aim, optional: true
belongs_to :entity, polymorphic: true, optional: true
belongs_to :reward_expense, inverse_of: :aim_entity, optional: true
belongs_to :aim_user, ->(o){ where(aim_id: o.aim_id, serial_number: o.serial_number) }, primary_key: :user_id, foreign_key: :user_id, counter_cache: true, optional: true
belongs_to :aim_user, ->(o){ where(o.filter_hash) }, primary_key: :user_id, foreign_key: :user_id, counter_cache: true, optional: true
belongs_to :reward, ->(o){ where(entity_type: o.entity_type) }, primary_key: :entity_id, foreign_key: :entity_id, optional: true

has_many :aim_logs, ->(o){ where(user_id: o.user_id, entity_type: o.entity_type, entity_id: o.entity_id) }, foreign_key: :aim_id, primary_key: :aim_id

validates :user_id, presence: true, uniqueness: { scope: [:aim_id, :serial_number, :entity_type, :entity_id] }, if: -> { ip.blank? }
validates :ip, presence: true, uniqueness: { scope: [:aim_id, :serial_number, :entity_type, :entity_id] }, if: -> { user_id.blank? }
# validates :ip, presence: true, uniqueness: { scope: [:aim_id, :entity_type, :entity_id] }, if: -> { user_id.blank? }

before_create :init_aim_user
after_create_commit :sync_aim_user_state
Expand All @@ -37,12 +32,6 @@ module Model::AimEntity
}
end

def filter_hash
{
text_year: text_year
}
end

def init_aim_user
self.aim_user || create_aim_user if self.user_id
if aim.reward_point == 0
Expand Down
6 changes: 1 addition & 5 deletions app/models/growth/model/aim_user.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
module Growth
module Model::AimUser
extend ActiveSupport::Concern
include Inner::Text

included do
attribute :text_year, :string
attribute :text_month, :string
attribute :state, :string, default: 'task_doing'
attribute :reward_amount, :integer, default: 0
attribute :aim_entities_count, :integer, default: 0

belongs_to :user, class_name: 'Auth::User', optional: true

belongs_to :aim, optional: true
has_many :aim_entities, ->(o){ where(user_id: o.user_id) }, foreign_key: :aim_id, primary_key: :aim_id

enum state: {
Expand Down

0 comments on commit 24a2394

Please sign in to comment.