Sha256: 0f1fc9132924f53120f17f53349aa8623b2d4c425732ae42befaf418d82372c7

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

module ActsAsCommentableMore
  module Helpers
    module Comment
      module InstanceMethodsHelper

        private

        def comment_define_instance_method

          comment_model.class_eval do
            def related_attributes
              read_attribute(:related_attributes).symbolize_keys
            end
          end

        end

        def comment_define_class_method
          
          comment_model.class_eval do

            class << self

              def find_comments_by_user(user, role = nil)
                attr_finder = { user: user }
                attr_finder.merge!(role: role.to_s) if role.present?
                where(attr_finder).order(created_at: :desc)
              end

            end
          end

        end

        def comment_define_can_change_role_of(commentable_name)
          comment_model.redefine_method("can_change_role?") do |role|
            commentable_class = send(commentable_name).class
            limit_role = commentable_class.comment_roles
            limit_role.include?(role.to_sym)
          end
          comment_model.send(:private, "can_change_role?".to_sym)
        end

        def comment_define_is_role?(role)
          comment_model.redefine_method("is_#{role}?") do
            raise(NoMethodError, "undefined method 'is_" + role.to_s + "?'") unless can_change_role?(role.to_s)
            self.role == role.to_s
          end
        end

        def comment_define_to_role(role)
          comment_model.redefine_method("to_#{role}") do
            raise(NoMethodError, "undefined method 'is_" + role.to_s + "?'") unless can_change_role?(role.to_s)
            self.role = role.to_s
            self
          end
        end

        def comment_define_to_role!(role)
          comment_model.redefine_method("to_#{role}!") do
            raise(NoMethodError, "undefined method 'is_" + role.to_s + "?'") unless can_change_role?(role.to_s)
            self.update(role: role.to_s)
            self
          end
        end


      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acts_as_commentable_more-1.2.7 lib/acts_as_commentable_more/helpers/comment/instance_methods_helper.rb
acts_as_commentable_more-1.2.6 lib/acts_as_commentable_more/helpers/comment/instance_methods_helper.rb