Last active
December 7, 2017 08:06
-
-
Save yhirano55/b3eb88c05877fb27b0b08de043317c51 to your computer and use it in GitHub Desktop.
関心事の分離とアンチパターン ref: https://qiita.com/yhirano55/items/92ba558bcb19f3a1f233
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Todo < ApplicationRecord | |
# Other todo implementation | |
# ... | |
## Event tracking | |
has_many :events | |
before_create :track_creation | |
after_destroy :track_deletion | |
private | |
def track_creation | |
# ... | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Todo < ApplicationRecord | |
# Other todo implementation | |
# ... | |
module EventTracking | |
extend ActiveSupport::Concern | |
included do | |
has_many :events | |
before_create :track_creation | |
after_destroy :track_deletion | |
end | |
private | |
def track_creation | |
# ... | |
end | |
end | |
include EventTracking | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Todo < ApplicationRecord | |
# Other todo implementation | |
# ... | |
include TodoEventTracking | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Todo | |
# Other todo implementation | |
# ... | |
concerning :EventTracking do | |
included do | |
has_many :events | |
before_create :track_creation | |
after_destroy :track_deletion | |
end | |
private | |
def track_creation | |
# ... | |
end | |
end | |
end | |
Todo.ancestors | |
# => Todo, Todo::EventTracking, Object |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Module | |
module Concerning | |
# Define a new concern and mix it in. | |
def concerning(topic, &block) | |
include concern(topic, &block) | |
end | |
def concern(topic, &module_definition) | |
const_set topic, Module.new { | |
extend ::ActiveSupport::Concern | |
module_eval(&module_definition) | |
} | |
end | |
end | |
include Concerning | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment