Module: OldieRailsModels::Model

Defined in:
lib/oldie_rails_models/model.rb

Instance Method Summary collapse

Instance Method Details

#attr_accessible(*args) ⇒ Object



93
# File 'lib/oldie_rails_models/model.rb', line 93

def attr_accessible(*args); end

#before_validation_on_create(*args) ⇒ Object



77
78
79
# File 'lib/oldie_rails_models/model.rb', line 77

def before_validation_on_create(*args)
  before_validation *add_parameter(args, { on: :create })
end

#before_validation_on_update(*args) ⇒ Object



81
82
83
# File 'lib/oldie_rails_models/model.rb', line 81

def before_validation_on_update(*args)
  before_validation *add_parameter(args, { on: :update })
end

#default_scope(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/oldie_rails_models/model.rb', line 21

def default_scope(*args)
  opts, block = args

  super ->(*p) {

    h = case opts
    when Hash
      opts
    when Proc
      opts.call(*p)
    end

    parent_scoped(h, self)
  }

end

#has_many(*args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/oldie_rails_models/model.rb', line 95

def has_many(*args)
  name, opts = args
  if opts
    finders = {}
    [:conditions, :order, :joins, :limit, :include, :offset, :group, :having, :finder_sql].each do |k|
      if opts[k]
        finders[k] = opts[k]
        opts.delete(k)
      end
    end
    super(name, -> { parent_scoped(finders, self) }, opts)
  else
    super(name, opts)
  end
end

#named_scope(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/oldie_rails_models/model.rb', line 4

def named_scope(*args)
  name, opts, block = args

  scope name, ->(*p) {

    h = case opts
    when Hash
      opts
    when Proc
      opts.call(*p)
    end

    parent_scoped(h, self)
  }

end

#parent_scoped(h, parent) ⇒ Object



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
# File 'lib/oldie_rails_models/model.rb', line 42

def parent_scoped(h, parent)
  h.inject(parent) do |s, (key, value)|
    case key
    when :conditions
      s.where(value)
    when :select
      s.select(value)
    when :order
      s.order(value)
    when :joins
      s.joins(value)
    when :limit
      s.limit(value)
    when :includes
      s.includes(value)
    when :offset
      s.offset(value)
    when :group
      s.group(value)
    when :having
      s.having(value)
    when :finder_sql
      s.find_by_sql(value)
    end
  end
end

#scoped(h) ⇒ Object



38
39
40
# File 'lib/oldie_rails_models/model.rb', line 38

def scoped(h)
  parent_scoped(h, self)
end

#set_primary_key(*args) ⇒ Object



85
86
87
# File 'lib/oldie_rails_models/model.rb', line 85

def set_primary_key(*args)
  # self.primary_key = *args
end

#set_table_name(*args) ⇒ Object



89
90
91
# File 'lib/oldie_rails_models/model.rb', line 89

def set_table_name(*args)
  self.table_name = *args
end

#validate_on_create(*args) ⇒ Object



69
70
71
# File 'lib/oldie_rails_models/model.rb', line 69

def validate_on_create(*args)
  validate *add_parameter(args, { on: :create })
end

#validate_on_update(*args) ⇒ Object



73
74
75
# File 'lib/oldie_rails_models/model.rb', line 73

def validate_on_update(*args)
  validate *add_parameter(args, { on: :update })
end