Giter VIP home page Giter VIP logo

acts_as_history's Introduction

= acts_as_history

This plugin is nice utility for storing objects in database. 
You can use it for ActiveRecord::Base or ActiveRecord::BaseWithoutTable models.

This is beta version. Please check and let me know if something doesn't work ;)

== Install
  
  Plugin generator will generate migrations for table 'histories'.
  
  Migration
  
  ./script/generate acts_as_history
  rake db:migrate

== Examples

1. Create your own class in History namespace

class History::CompanyHistory < History
  # here you need to define serialized class
  Company
end


2. Declare acts_as_history in Company model

class Company < ActiveRecord::Base
  acts_as_history :delete_from => :week  # config {:day, :week, :month, :year}
  after_save :write_history
  
  # set associations
  has_many   :company_histories, :as => :owner, :class_name => 'History::CompanyHistory', :dependent => :destroy  
  has_many   :setting_histories, :as => :owner, :class_name => 'History::CompanySetting', :dependent => :destroy
  
  # this method will return setting history for period
  def histories_for_period(period)
    self.setting_histories.find_first_by_date(period.time.end_of_month).value
  end
  # other methods... 
end


Other methods are inherited from History class or included from vendor/plugin/acts_as_history/lib/acts_as_history.rb 


3. Using 

a) reading history :

>> Company.find(1).company_histories.first.value
  Company Load (0.3ms)   SELECT * FROM `companies` WHERE (`companies`.`id` = 1) 
  History::CompanyHistory Load (0.4ms)   SELECT * FROM `histories` WHERE (`histories`.owner_id = 1 AND `histories`.owner_type = 'Company') AND ( (`histories`.`type` = 'History::CompanyHistory' ) ) LIMIT 1
 => #<Company id: 1, activated: true, locked: nil, accounting_office_id: nil, address: "foo", city: nil, name: "example company name", nip: "xxx-xxx-xx-xx", postal_code: "62-800", regon: "320816455", bank_account: nil, created_at: "2010-05-07 11:43:38", updated_at: "2010-05-07 11:43:38", city_id: 832, natural_person: true, to_destroy: nil, tax_office_id: nil, country_id: 161>

>> Company.first.read_history_for_object(SaleInvoice.find(3),:updated_at)

>> Company.first.read_history_by_date Time.now

b) write to history 

>> a = History::CompanyHistory.new(:owner => Company.first, :value => Company.first, :date =>Time.now)
>> a.save

c) checking history status for date

>>Company.first.has_history_from? Time.now - 1.day
=> true

== Contributors

Michał Górski, http://www.goorsky.info

== Contact

Any bugs, questions, comments please feel free to email me : [email protected]

Copyright (c) 2010,  Boguslaw Tarnowski, released under the MIT license

acts_as_history's People

Contributors

ubik86 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.