Class: Elibri::Connect::DbStructureGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/elibri/connect/db_structure/db_structure_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(path) ⇒ Object



122
123
124
# File 'lib/generators/elibri/connect/db_structure/db_structure_generator.rb', line 122

def self.next_migration_number(path)
  Time.now.utc.strftime("%Y%m%d%H%M%S")
end

Instance Method Details

#create_elibri_db_structureObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/generators/elibri/connect/db_structure/db_structure_generator.rb', line 12

def create_elibri_db_structure
  create_file "app/models/contributor.rb", 
    %Q{class Contributor < ActiveRecord::Base

belongs_to :product, :touch => true

attr_accessible :import_id, :role_name, :role, :from_language, :full_name, :title, :first_name, 
                    :last_name_prefix, :last_name, :last_name_postfix, :biography
    end}
  create_file "app/models/product_text.rb",
    %Q{class ProductText < ActiveRecord::Base

belongs_to :product, :touch => true

attr_accessible :import_id, :text, :text_type, :text_author, :source_title, :resource_link

    end}
    
  create_file "app/models/related_product.rb",
%Q{class RelatedProduct < ActiveRecord::Base
  attr_accessible :onix_code, :product_id, :related_record_reference

  belongs_to :product

  def object
    Product.where(:record_reference => related_record_reference).first
  end

  def self.objects
    joins(:product).first.product.related_products.map { |x| Product.where(:record_reference => x.related_record_reference).first }.compact
  end

end}
  
  create_file "app/models/product.rb",
    %Q[class Product < ActiveRecord::Base

with_options :autosave => true, :dependent => :destroy do |product|
  product.has_many :contributors
  product.has_many :product_texts
  product.has_one :imprint
  product.has_many :related_products
end

HEIGHT_UNIT = 'mm'
WIDTH_UNIT = 'mm'
THICKNESS_UNIT = 'mm'
WEIGHT_UNIT = 'gr'
FILE_SIZE_UNIT = 'MB'
DURATION_UNIT = 'min'

attr_accessible :isbn, :title, :full_title, :trade_title, :original_title, :publication_year,
                :publication_month, :publication_day, :number_of_pages, :width, :height, 
                :cover_type, :edition_statement, :audience_age_from, :audience_age_to, 
                :price_amount, :vat, :current_state, :product_form, :old_xml

#on left side elibri attributes, on right side our name of attribute               
acts_as_elibri_product :record_reference => :record_reference, #very important attribute!
       :isbn13 => :isbn,
       :title => :title,
       :full_title => :full_title,
       :trade_title => :trade_title,
       :original_title => :original_title,
       :number_of_pages => :number_of_pages,
       :duration => :duration,
       :width => :width,
       :height => :height,
       :cover_type => :cover_type,
       :pkwiu => :pkwiu,
       :edition_statement => :edition_statement,
       :cover_price => :price_amount,
       :vat => :vat,
       :product_form => :product_form,
       :no_contributor? => :no_contributor,
       :unnamed_persons? => :unnamed_persons,
       :contributors => { #name in elibri
         :contributors => { #name in our db
           :id => :import_id,
           :role_name => :role_name,
           :role => :role,
           :from_language => :from_language,
           :person_name => :full_name,
           :titles_before_names => :title,
           :names_before_key => :first_name,
           :prefix_to_key => :last_name_prefix,
           :key_names => :last_name,
           :names_after_key => :last_name_postfix,
           :biographical_note => :biography
         }
       },
       :related_products => {
          :related_products => {
            :record_reference => :related_record_reference,
            :relation_code => :onix_code
          }
        },
       :text_contents => {
         :product_texts => {
           :id => :import_id,
           :text => :text,
           :type_name => :text_type,
           :author => :text_author,
           :source_title => :source_title,
           :source_url => :resource_link
         }
       }
    ]
  migration_template "create_elibri_structure.rb", "db/migrate/create_elibri_structure.rb"
end