ActiveAdmin计算

时间:2014-06-05 07:01:36

标签: ruby ruby-on-rails-4 activeadmin

我想对我的管理界面实施一些计算,所以我有一个产品资源,在这个资源上你会看到我做的服务列表,例如喷涂,应用程序的价格被视为例如(每1平方厘米1美元)。

我怎样才能更好地实现这个想法?

我希望看到用户按下按钮"新产品" 这是一个他写下平方厘米数的字段,并根据这些尺寸,它会自动以货币呈现所需金额。


Rails 4.1.0

ActiveAdmin 1.0.0

ruby​​ 2.1


刚才您只能输入固定价格,例如1个产品/服务的固定价格。

应用/管理/ product.rb

ActiveAdmin.register Product, { :sort_order => :name_asc } do

    # Scopes
    scope :all, :default => true
    scope :available do |products|
        products.where("available < ?", Date.today)
    end
    scope :drafts do |products|
        products.where("available > ?", Date.today)
    end
    scope :featured_products do |products|
        products.where(:featured => true)
    end

    # Permitted parameters
    permit_params :article_id, :title, :description, :price, :featured, :available, :image_file_name

    # Displayed columns
    index do
    selectable_column
        column :article,  :sortable => :article
        column :title,    :sortable => :title
        column :description
    # Currency helper
        column :price, :sortable => :price do |cur|
            number_to_currency cur.price, locale: :ru
        end
        column :featured
        column :available
    #   column :image_file_name
        actions
    end

    # Product details
    show do
        panel "Product Details" do
            attributes_table_for product do
                row("Article")           { link_to product.article }
                row("Title")             { product.title }
                row("Description")       { product.description }
                row("Price")             { product.price }
                row("Featured")          { product.featured }
                row("Available on")      { product.available }
                row("Image")             { image_tag("products/" + product.image_file_name) }
            end
        end
    end
    # Filters
    filter :article,      :as => :select
    filter :title,        :as => :select               # :check_boxes (for checkboxes)
    filter :price,        :as => :select
    filter :available,    :as => :select
    filter :featured,     :as => :check_boxes
end

应用/模型/ product.rb

class Product < ActiveRecord::Base

    # Relationship
    belongs_to :article

    # Named Scopes
    scope :available, lambda{ where("available < ?", Date.today) }
    scope :drafts,    lambda{ where("available > ?", Date.today) }

    # Validations
    validates :article, :title, :description, :price, :available, :presence => true
    validates :featured, :inclusion => { :in => [true, false] }
end

应用/模型/ article.rb

class Article < ActiveRecord::Base

    # Relationship
    has_many :products, :dependent => :delete_all

    # Validations
    validates :title, :description, :presence => true

    # Define for display a article for products as article code
    def to_s
        "#{title}"
    end
end

0 个答案:

没有答案