Rails,写模块。存储设置在哪里?

时间:2015-10-23 15:04:17

标签: ruby-on-rails ruby module

我有模特的行为。

我想包括这样的内容:

class Building < ActiveRecord::Base
    has_many :blocks
    include Staticizable
    check_children :blocks #here name of children model to check
    ...
end

行为如下:

module Staticizable
    extend ActiveSupport::Concern
    module ClassMethods
        def check_children child
            self.child_model = child #where is to store model name to check?
        end
    end

    def was_staticized
        (DateTime.now - self.staticized_date.to_datetime).to_i
    end

    def staticized?
        if @child_model.present?
            ap self.send(@child_model)
        else
            if staticize_period > was_staticized
                true
            else
                false
            end
        end
    end

    def staticized
        self.staticized_date = DateTime.now
        save
    end
end

所以我需要知道在哪里可以存储模型名称来检查孩子。做这样的事情的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

最后,我得到了答案,不确定这是最好的方法,如果不是,请告诉我,请:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    >
    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar"
    />
    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    app:headerLayout="@layout/header"
    app:menu="@menu/drawer"
    />
</android.support.v4.widget.DrawerLayout>

此外,我已使用

在/ config / initializers中创建了一个文件
module Staticizable
    extend ActiveSupport::Concern

    included do
        class << self;
            attr_accessor :has_child_custom
        end
    end

    module ClassMethods
        def check_children child
            self.has_child_custom = child
        end
    end

    def was_staticized
        unless staticized_date
            return false
        end
        (DateTime.now - self.staticized_date.to_datetime).to_i
    end

    def staticized?
        if self.class.has_child_custom.present?
            self.send(self.class.has_child_custom).each do |child|
                unless child.staticized?
                    return false
                end
            end
        else
            if self.staticize_period > was_staticized
                true
            else
                false
            end
        end

    end

    def staticized
        self.staticized_date = DateTime.now
        save
    end

end

并在模型中设置:

ActiveRecord::Base.send :include, Staticizable