Sha256: 9fa475d2316a9bc79a58ceeee7a91e7c1e2dfef94966e227e26222715d1d7710
Contents?: true
Size: 1.18 KB
Versions: 20
Compression:
Stored size: 1.18 KB
Contents
class Book < Content numbered :chapters aggregate_of :chapters has_many :chapters, -> { order(number: :asc) }, dependent: :delete_all has_many :complements, dependent: :delete_all delegate :first_lesson, to: :first_chapter def to_s slug end def first_chapter chapters.first end def next_lesson_for(user) user.try(:last_lesson)|| first_lesson end def import_from_json!(json) self.assign_attributes json.except('chapters', 'complements', 'id', 'description', 'teacher_info') self.description = json['description'].squeeze(' ') rebuild! json['chapters'].map { |it| Topic.find_by!(slug: it).as_chapter_of(self) } rebuild_complements! (json['complements']||[]).map { |it| Guide.find_by!(slug: it).as_complement_of(self) } Organization.all.each { |org| org.reindex_usages! } end def rebuild_complements!(complements) #FIXME use rebuild transaction do self.complements.all_except(complements).delete_all self.update! :complements => complements complements.each &:save! end reload end def index_usage!(organization) [chapters, complements].flatten.each { |item| item.index_usage! organization } end end
Version data entries
20 entries across 20 versions & 1 rubygems